This article is mainly to share with you PHP object-oriented private, protected, public, the difference between the three, I hope to help everyone.
Private protected public This class within y y y sub-class n y y outer n n y
Attention:
In Java, if no arguments are written before the property/method
That Public/protected/private is not written, is also possible, friendly
Class Human {Private $name = ' Zhangsan '; protected $money = 3000; Public $age = 28; Public function say () {echo ' My name ', $this->name, ' <br/> '; Echo ' I have ', $this->money, ' Yuan Qian <br/> '; Echo ' I this year ', $this->age, ' old '; }}class Stu extends Human {private $friend = ' floret '; Public function Talk () {echo ' My name ', $this->name, ' <br/> '; Echo ' I have ', $this->money, ' Yuan Qian <br/> '; Echo ' I this year ', $this->age, ' old <br/> '; }} $ming = new Stu (); Echo $ming->age, ' <br/> '; 28echo $ming->friend; Error: Privateecho cannot be called outside the class $ming->money; Error: Protected property cannot be called Outside the class $ming->talk ();/** error: notice:undefined property:stu:: $name in ... I have 3000 dollars, I'm 28 years old. Analysis Reason: Undefined property:stu:: $name This is the description: Stu object does not have a name property but said yesterday, is not the private can inherit it? Yes, but the system has tags. Marks it as a private property at the parent level. Therefore, it is not authorized to call, which causes this error to occur. Can be analyzed: protected can be accessed within the subclass protected can be accessed in subclasses, can be accessed within this class? A: Of course.