Access control over properties or methods is implemented by adding the public, protected, or private keywords to the front. It is defined as a public class member that can be accessed anywhere. A protected class member can be accessed by itself, its subclass, and its parent class. A private class member can only be accessed by the class it defines. Access control over properties or methods is implemented by adding the public, protected, or private keywords to the front. It is defined as a public class member that can be accessed anywhere. A protected class member can be accessed by itself, its subclass, and its parent class. A private class member can only be accessed by the class it defines.
1. attribute access control
Class attributes must be defined as public, protected, and private. If var is used, it is regarded as public.
Example #1 attribute declaration
Class MyClass {public $ public = 'public'; protected $ protected = 'protected'; private $ private = 'private'; function printHello () {echo $ this-> Public .'
'; Echo $ this-> protected .'
'; Echo $ this-> private .'
';}}$ Obj = new MyClass (); echo $ obj-> public; // This line can run echo $ obj-> protected; // This line will generate a fatal error echo $ obj-> private; // This line will also generate a fatal error $ obj-> printHello (); // normally output values of public, protected, and private: class MyClass2 extends MyClass {protected $ protected = 'protected2 '; function printHello () {echo $ this-> public; echo $ this-> protected; echo $ this-> private; }}$ obj2 = new MyClass2 (); echo $ obj2-> public; // This line can be normally executed echo $ obj2-> private; // privateecho $ obj2-> protected is not defined; // a fatal error is generated $ obj2-> printHello (); // output Public, Protected, and Undefined
Note: For compatibility considerations, the method for defining variables using the var keyword in PHP 4 is still valid in PHP 5 (just as an alias for the public keyword ). In versions earlier than PHP 5.1.3, this syntax generates an E_STRICT warning.
Method access control
Classes can be defined as public, private, or protected. If these keywords are not set, the method is public by default.
Example #2 method declaration
Class MyClass {// declare a public constructor public function _ construct () {}// declare a public method public function MyPublic () {}// declare a protected method protected function MyProtected () {}// declare a private method private function MyPrivate () {}// this method is a public function Foo () {$ this-> MyPublic (); $ this-> MyProtected (); $ this-> MyPrivate ();}} $ myclass = new MyClass; $ myclass-> MyPublic (); // This line can be executed normally $ myclass-> MyProtected (); // This row generates a fatal error $ myclass-> MyPrivate (); // This row generates a fatal error $ myclass-> Foo (); // public, protected, private, you can execute class MyClass2 extends MyClass {// this method is public function Foo2 () {$ this-> MyPublic (); $ this-> MyProtected (); $ this-> MyPrivate (); // this row generates a fatal error} $ myclass2 = new MyClass2; $ myclass2-> MyPublic (); // This row can be normally executed $ myclass2-> Foo2 (); // both public and protected can be executed, but private class Bar {public function test () {$ this-> testPrivate (); $ this-> testPublic ();} public function testPublic () {echo "Bar: testPublic
";} Private function testPrivate () {echo" Bar: testPrivate
";}} Class Foo extends Bar {public function testPublic () {echo" Foo: testPublic
";} Public function testPrivate () {echo" Foo: testPrivate
";}}$ MyFoo = new Foo (); $ myFoo-> test (); // Bar: testPrivate // Foo: testPublic
Access control for other objects
Objects of the same class can access each other's private and protected members even if they are not the same instance. This is because the specific implementation details of these objects are known.
Example #3 access a private member of the same object type
Class Test {private $ foo; public function _ construct ($ foo) {$ this-> foo = $ foo;} private function bar () {echo "Accessed the private method. ";} public function baz (Test $ other) {// Here we can change the value of the private attribute $ other-> foo = 'hello '; var_dump ($ this-> foo); // Here we can also call the private method $ other-> bar ();}} $ test = new Test ('test'); $ test-> baz (new test ('other '));
Output result:
String (4) "test"
Accessed the private method.