How to make so that deduced and protected and closed property?

class MyClass { protected $prot = "text1"; private $priv = "text2"; function printText() { echo $this->prot; echo $this->priv; } } $obj = new MyClass(); echo $obj->prot; echo $obj->priv; $obj->printText(); 

    1 answer 1

    Properties and methods marked as protected or protected are available only to the source class itself and its heirs.

    This modifier is usually used if it is assumed that inheritance classes should have access to certain members of the base class, but accessing them from any other classes ( public ) is not desirable.

    Some examples of use in PHP can be found in English SO.

    Private members of the class ( private ) are available only within itself.

    Therefore, in your case it is:

     $obj->printText(); 

    It will work fine. And this is:

     echo $obj->prot; echo $obj->priv; 

    Will give an error.