Php object-oriented programming-class access modifier

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

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.