Php object-oriented programming (OOP)-class access modifiers allow developers to restrict access to class members. this is a new feature of PHP5.
Private |
Protected |
Public |
| In the same class |
√ |
√ |
√ |
| In the subclass of the class |
|
√ |
√ |
| All external members |
|
|
√ |
Public; echo $ this-> protected; echo $ this-> private; }}$ obj = new MyClass (); echo $ obj-> public; // Worksecho $ obj-> protected; // Fatal Errorecho $ obj-> private; // Fatal Error $ obj-> printHello (); // Shows Public, protected and Private/*** Define MyClass2 */class MyClass2 extends MyClass {// We can redeclare the public and protected method, but not privateprotected $ protected = 'protected2 '; function printHello () {Echo $ this-> public; echo $ this-> protected; echo $ this-> private; }}$ obj2 = new MyClass2 (); echo $ obj-> public; // Worksecho $ obj2-> private; // Undefinedecho $ obj2-> protected; // Fatal Error $ obj2-> printHello (); // Shows Public, Protected2, not Private?>
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 extends MyClass {// This is publicfunction Foo2 () {$ this-> MyPublic (); $ this-> MyProtected (); $ this-> MyPrivate (); // Fatal Error }}$ myclas S2 = new MyClass2; $ myclass2-> MyPublic (); // Works $ myclass2-> Foo2 (); // Public and Protected work, not Private?>