In PHP, the access control of the property or method is implemented in front of the public private protected and other keywords, public decorated class members can be accessed anywhere, private decorated class members can only be accessed by themselves, Protected decorated class members can be accessed by themselves or by their subclasses.
classtest{ Public$public=' Public ';Private$private=' Private ';protected$protected=' protected '; Public functionshow(){Echo$this-Private; } Public functionshow2(){Echo$this-protected; } }$test=NewTest ();Echo$test- Public;$test->show ();$test->show2 ();//exit ();//Echo $test->protected; will produce a fatal error//Echo $test->private; It also produces a fatal error. The private property can only be accessed in defined classes/** * */ classtest2extendstest {//Override the show () method of the parent class functionshow(){//inherit from protected of parent classEcho$this-protected; } }$test 2=NewTest2 ();Echo"
";//$test 2->protected;//will produce a fatal error protected property cannot be accessed externally$test 2->show ();//access to protected can only be accessed by itself or its subclasses?>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP access control (public protected private), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.