[]php The difference between public, protected, and private three access control modes

Source: Internet
Author: User

From:http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading

Public: Common Type

The public method or property can be called through Self::var in a subclass, Parent::method calling the parent class method

A method or property in an instance that can be $obj->var to invoke the public type

Protected: Protected Type
The protected method or property can be called by Self::var in a subclass, Parent::method calling the parent class method

Methods or properties of the protected type cannot be called through $obj->var in an instance

Private: Proprietary type
A property or method of that type can only be used in that class, and the properties and methods of private types cannot be called in instances, subclasses, or instances of subclasses of that class


The difference between 2.self and parent
A). These two pairs of images are commonly used in subclasses. The main difference is that self can invoke a public or protected property in the parent class, but parent cannot call

b). Self:: It indicates that static members (methods and properties) of the current class are different from $this, $this refers to the current

Attached code:

1<?PHP2 /**3 * Parent can only call public or protected methods in the parent class and cannot invoke properties in the parent class4 * Self can call all data except methods and properties of private types in the parent class5  */6 classuser{7      Public $name;8     Private $passwd;9     protected $email; Ten      Public  function__construct () { One         //print __class__. " "; A         $this->name= ' Simple '; -         $this->passwd= ' 123456 '; -         $this->email = ' [email protected] '; the     }     -      Public functionShow () { -         Print"Good"; -     }     +      Public functioninuserclasspublic () { -         Print __class__.‘::‘.__function__." "; +     }     A     protected  functioninuserclassprotected () { at         Print __class__.‘::‘.__function__." "; -     }     -     Private functioninuserclassprivate () { -         Print __class__.‘::‘.__function__." "; -     } - } in  - classSimpleuserextendsUser { to      Public function__construct () { +         //print __class__. " "; -Parent::__construct (); the     } *      $      Public functionShow () {Panax Notoginseng         Print $this->name. " Public ";  -         Print $this->passwd. " Private "; the         Print $this->email. " Protected "; +     } A      the      Public functioninsimpleuserclasspublic () { +         Print __class__.‘::‘.__function__." "; -     } $      $     protected functioninsimpleuserclassprotected () { -         Print __class__.‘::‘.__function__." "; -     } the      -     Private functioninsimpleuserclassprivate () {Wuyi         Print __class__.‘::‘.__function__." "; the     } - } Wu  - classAdminUserextendsSimpleuser { About     protected $admin _user; $      Public function__construct () { -         //print __class__. " "; -Parent::__construct (); -     } A      +      Public functioninadminuserclasspublic () { the         Print __class__.‘::‘.__function__." "; -     } $      the     protected functioninadminuserclassprotected () { the         Print __class__.‘::‘.__function__." "; the     } the      -     Private functioninadminuserclassprivate () { in         Print __class__.‘::‘.__function__." "; the     } the } About  the classAdministratorextendsAdminUser { the      Public function__construct () { theParent::__construct (); +     } - } the Bayi /** the * Only public properties and methods in an instance of a class can be instantiated to invoke the  */ - $s=Newadministrator (); - Print‘-------------------‘; the $s-Show (); the?>

[]php The difference between public, protected, and private three access control modes

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.