0

I am beginner in coding.I want to learn the concept of traits in php.I have a doubt that "does the concept of traits has similar meaning in both java and php"?If there are any differences what are they?

1 Answers1

0

Why use traits in PHP?

PHP provides single class inheritance not multiple so whenever your class needs to inherit functionality from multiple classes simple use traits.

trait Abc
{

     private static $_instance = null;

    public static function getInstance()
    {
      if (null === self::$_instance)
      {
        self::$_instance = new self();
      }

    return self::$_instance;
   }
}

class Test extends Example
{
  use Abc;
  ....
}

In Java, it supports multiple inheritances.so whenever a class needs just extend multiple classes but at that time you may face one problem that is shown in this question.

Dhaval
  • 1,393
  • 5
  • 29
  • 55
  • **Wrong,** Java does not support "multi" inheritance (but has "single" inheritance), and will not ever support C++'s multi-inheritance, because Java is a dead language (at least, it's dead as far as improvement is concerned, see my years old bug report: https://stackoverflow.com/q/48203021/8740349). – Top-Master Jun 01 '22 at 16:15