Can traits have properties & methods with private & protected visibility? Can traits have constructor, destructor & class-constants?

14,681

Traits can have properties and methods with private and protected visibility too. You can access them like they belong to class itself. There is no difference.

Traits can have a constructor and destructor but they are not for the trait itself, they are for the class which uses the trait.

Traits cannot have constants. There is no private or protected constants in PHP before version 7.1.0.

trait Singleton{
    //private const CONST1 = 'const1'; //FatalError
    private static $instance = null;
    private $prop = 5;

    private function __construct()
    {
        echo "my private construct<br/>";
    }

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

    public function setProp($value)
    {
        $this->prop = $value;
    }

    public function getProp()
    {
        return $this->prop;
    }
}

class A
{
    use Singleton;

    private $classProp = 5;

    public function randProp()
    {
        $this->prop = rand(0,100);
    }

    public function writeProp()
    {
        echo $this->prop . "<br/>";
    }
}

//$a1 = new A(); //Fatal Error too private constructor
$a1 = A::getInstance();
$a1->writeProp();
echo $a1->getProp() . "<br/>";
$a1->setProp(10);
$a1->writeProp();
$a1->randProp();
$a1->writeProp();
$a2 = A::getInstance();
$a2->writeProp();
$a2->randProp();
$a2->writeProp();
$a1->writeProp();
Share:
14,681

Related videos on Youtube

Admin
Author by

Admin

Updated on September 16, 2022

Comments

  • Admin
    Admin over 1 year

    I've never seen a single trait where properties and methods are private or protected.

    Every time I worked with traits I observed that all the properties and methods declared into any trait are always public only.

    Can traits have properties and methods with private and protected visibility too? If yes, how to access them inside a class/inside some other trait? If no, why?

    Can traits have constructor and destructor defined/declared within them? If yes, how to access them inside a class? If no, why?

    Can traits have constants, I mean like class constants with different visibility? If yes, how to inside a class/inside some other trait? If no, why?

    Special Note : Please answer the question with working suitable examples demonstrating these concepts.

  • AnrDaemon
    AnrDaemon over 5 years
    Traits can have constants, but you will not be able to redefine them in the class or its descendants.
  • Hmorv
    Hmorv about 5 years
    No, Traits can't have constants, see this