Questions about php access control-php Tutorial

Source: Internet
Author: User
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 !!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.