The access modifiers of the type allow developers to restrict access to class members, which is a new feature of PHP5
Private |
Protected |
Public |
In the same class |
√ |
√ |
√ |
Class in the subclass of the |
|
√ |
√ |
All the external members |
|
|
√ |
Public;echo $this->protected;echo $this->private;}} $obj = new MyClass (), Echo $obj->public;//worksecho $obj->protected;//Fatal errorecho $obj->private;//Fatal Er Ror$obj->printhello ();//shows public, Protected and private/** * Define MyClass2 */class MyClass2 extends myclass{//W E can redeclare the public and protected method, but not privateprotected $protected = ' Protected2 '; function Printhello () { echo $this->public;echo $this->protected;echo $this->private;}} $obj 2 = new MyClass2 (), Echo $obj->public;//worksecho $obj 2->private;//undefinedecho $obj 2->protected;// Fatal Error$obj2->printhello ();//shows public, Protected2, not private?>
!--? php/** * Class method access control * Define MyClass */class myclass{//contructors must be publicpublic function _ _construct () {}//Declare a public methodpublic function Mypublic () {}//Declare a protected methodprotected function My Protected () {}//abstract Protected function Funprotect ($param); Abstract methods can only be defined in abstract classes//Declare a private methodprivate fun Ction myprivate () {}//This is Publicfunction Foo () {$this--->mypublic (); $this->myprotected (); $this->myprivate ( );}} $myclass = new MyClass; $myclass->mypublic ();//works$myclass->myprotected ();//Fatal error$myclass-> Myprivate ();//Fatal Error$myclass->foo ();//Public, Protected and Private work/** * Define MyClass2 */class MyClass2 E Xtends myclass{//This is Publicfunction Foo2 () {$this->mypublic (); $this->myprotected (); $this->myprivate () ;//Fatal Error}} $myclass 2 = new MyClass2; $myclass 2->mypublic ();//Works$myclass2->foo2 ();//Public and Protected work, not private?>