Questions about php access control
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 (); $ obj-> printHello (); class MyClass2 extends MyClass {function printHello () {echo $ this-> public; echo $ this-> protected; echo $ this-> private."
";}}$ Obj2 = new MyClass2 (); $ obj2-> printHello (); // output Public, Protected, and Undefinedclass MyClass3 extends MyClass {}$ obj3 = new MyClass3 (); $ obj3-> printHello (); // output Public, Protected, and private
Why $ obj3-> printHello's private has a value. if it is not a private-defined class member, it can only be accessed by its class.
Reply to discussion (solution)
In MyClass2, printHello overwrites printHello in MyClass.
The $ private of MyClass2 calling MyClass is undefined because the printHello method of MyClass2 does not have the permission to read the $ privite of MyClass.
No printHello exists in MyClass3, so $ obj3-> printHello actually calls the printHello of MyClass.
The MyClass printHello method has the permission to read Myclass $ private. Therefore, $ obj3-> printHello can display $ private of MyClass.
In MyClass2, printHello overwrites printHello in MyClass.
The $ private of MyClass2 calling MyClass is undefined because the printHello method of MyClass2 does not have the permission to read the $ privite of MyClass.
No printHello exists in MyClass3, so $ obj3-> printHello actually calls the printHello of MyClass.
The MyClass printHello method has the permission to read Myclass $ private. Therefore, $ obj3-> printHello can display $ private of MyClass.
Is it because the printHello method of MyClass3 inherits from MyClass, the printHello method is public, and the printHello in the MyClass3 heap still points to the printHello position of MyClass.
Upstairs !!!!