Php object-oriented syntax 4: access modifier, public, protected, private

Source: Internet
Author: User
Access modifier: used to describe the access modifier where a member (attribute, method) can be accessed:
Used to describe where a member (attribute, method) can be accessed!

Tip:
PHP adopts the concept of class to restrict access to members!
PHP divides the Accessed code into three areas: class, class, and inheritance chain!

Is based on:
Where the target member is defined and where the target member is accessed (check the current access-> where the property/method code is executed) to determine the class, out-of-class or within the inheritance chain class!

 Property) ;}} class B extends A {// define public $ property = 'phpphp' in class B; public function iam_ B () {var_dump ($ this-> property) ;}} class C extends B {public function iam_c () {var_dump ($ this-> property );}} /* public $ property = 'phpphp'; public: indicates that members in this class can be accessed within and outside the class on the inheritance chain! * /// Specify that the target member $ property is defined in Class B. $ C = new C; // $ c-> property; executed outside class B, you can access echo $ c-> property; echo'
 '; // $ This-> property in iam_c () is within the inheritance chain of class B and can be accessed to $ c-> iam_c (); // iam_ B () $ this-> property in Class B can be accessed to $ c-> iam_ B (); // iam_a () in $ this-> property is within the inheritance chain of class B, you can access $ c-> iam_a ();
Running result: string (8) "phpphphp" string (8) "phpphphp" string (8) "phpphphp" string (8) "phpphphp"

Protected $ property = 'phpphp ';
Protected means that members can be accessed within the class of the inheritance chain (subclass, parent class) within this class.

$ C = new C; // $ c-> property; executed outside class B, cannot access var_dump ($ c-> property); // iam_c () in $ this-> property is within the inheritance chain of class B, you can access $ c-> iam_c (); // iam_ B () $ this-> property in Class B can be accessed to $ c-> iam_ B (); // iam_a () in $ this-> property is within the inheritance chain of class B, you can access $ c-> iam_a ();
Running result: PHP Fatal error: Cannot access protected property C: $ property in/usercode/file. php on line 25

Private $ property = 'phpphp'
Private, which means that only within this class can be accessed!

$ C = new C; // $ c-> property; executed outside class B, cannot access var_dump ($ c-> property); // iam_c () in $ this-> property is within the inheritance chain of class B and cannot access $ c-> iam_c (); // iam_ B () $ this-> property in Class B can be accessed to $ c-> iam_ B (); // iam_a () in $ this-> property is within the inheritance chain of class B and cannot access $ c-> iam_a ();
Running result: NULLNULLstring (8) "phpphphp" NULLPHP Notice: Undefined property: C :$ property in/usercode/file. php on line 25 PHP Notice: Undefined property: C: $ property in/usercode/file. php on line 19 PHP Notice: Undefined property: C: $ property in/usercode/file. php on line 4

How to choose:
A principle that best reflects encapsulation. Encapsulation refers to hiding internal implementations as much as possible, and developing external operation interfaces only! In terms of syntax, all attributes and methods that do not require external use are private (protected), leaving only some necessary public methods!
A class must have at least one public method and can be called outside the class.
If a class is not intended to be inherited, the members can use private. If a class is a base class, it is designed to inherit from the quilt class, and some members will use protected. Let's look at the specific business requirements.

Tip:
1. generally, if a rewrite occurs, the location defined after the rewrite prevails.
(The exception is that private attributes cannot be overwritten when accessed in this class .)

 Property) ;}} class B extends A {// define public $ property = 'phpphp' in class B; public function iam_ B () {var_dump ($ this-> property) ;}} class C extends B {// class C defines the attribute public $ property = 'chongxiechongxiechongxieongxie'; public function iam_c () {var_dump ($ this-> property) ;}}$ c = new C; echo $ c-> property; echo'
 '; // Determines that $ this is a Class C object in the following three methods. First, check whether the class C has defined the property attribute. if not, search for the parent class. $ C-> iam_c (); $ c-> iam_ B (); $ c-> iam_a ();
Running result: chongxiechongxiechongxiestring (24) "chongxiechongxiechongxie" string (24) "chongxiechongxiechongxie" string (24) "chongxiechongxiechongxie"

Note: when rewriting the attributes or methods of the parent class, the access control modifier must be weaker than the attributes or methods of the parent class with the same name. Private is the strongest, and public is the weakest.

Exceptions:
Private attributes cannot be overwritten when accessed in this class.

 '; Var_dump ($ this); var_dump ($ this-> property); echo'
 ';}} Class B extends A {// define private $ property = 'phpphp' in class B; public function iam_ B () {echo' class access
'; Var_dump ($ this); var_dump ($ this-> property); echo' ';}} Class C extends B {// define the same name attribute public $ property = 'chongxiechongxiechongxie' in class C; public function iam_c () {echo' for access within the inheritance chain
'; Var_dump ($ this); var_dump ($ this-> property); echo' ';}}$ C = new C; echo 'Access outside the class'. $ c-> property; // access echo outside the class' '; $ C-> iam_c (); // access $ c-> iam_ B () in the inheritance chain; // access $ c-> iam_a () in class B (); // access within the inheritance chain
Running result: access chongxiechongxiechongxie outside the class and access string (24) "chongxiechongxiechongxie" object (C) #1 (2) in the inheritance chain) {["property"] => string (24) "chongxiechongxiechongxie" ["property": "B": private] => string (8) "phpphphp"} string (24) "chongxiechongxiechongxie" class access, cannot be overwritten, string (8) "phpphphp" if you know the reason, please leave a message to answer, thank you object (C) #1 (2) {["property"] => string (24) "chongxiechongxiechongxie" ["property": "B": private] => string (8) "phpphphp"} string (8) "phpphphp" access string (24) in the inheritance chain "chongxiechongxiechongxie" object (C) #1 (2) {["property"] => string (24) "chongxiechongxiechongxie" ["property": "B": private] => string (8) "phpphphp"} string (24) "chongxiechongxiechongxie"

The above is php object-oriented syntax 4: access modifier, public, protected, private content. For more information, see PHP Chinese network (www.php1.cn )!

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.