Differences between the public, protected, and private access control modes of php

Source: Internet
Author: User
Public: The public type can call the public method or attribute through self: var in the subclass. parent :: the method calls the parent class method. You can use $ obj-var in the instance to call the public type method or attribute protected: the protected type can be passed through self :: var calls the protected method or attribute. parent: method calls the parent class method.

Public: The public type can call the public method or attribute through self: var in the subclass. parent :: the method calls the parent class method. You can use $ obj-var in the instance to call the public type method or attribute protected: the protected type can be passed through self :: var calls the protected method or attribute. parent: method calls the parent class method.

Public: public type

In the subclass, you can use self: var to call the public method or attribute, and parent: method to call the parent method.

You can use $ obj-> var to call public methods or attributes in an instance.

Protected: protected type
In the subclass, you can use self: var to call the protected method or attribute, and parent: method to call the parent class method.

You cannot use $ obj-> var to call a protected method or attribute in an instance.

Private: private type
Attributes or methods of this type can only be used in this class. Private attributes and methods cannot be called in instances of this class, subclass, and subclass.


2. Differences between self and parent
A) These two pairs are often used in subclasses. Their main difference is that self can call public or protected attributes in the parent class, But parent cannot call

B). self: It indicates that the static member (method and attribute) of the current class is different from $ this. $ this indicates the current object.

Code:

/**
* Parent can only call public or protected methods in the parent class, and cannot call attributes in the parent class.
* Self can call all data except private methods and attributes in the parent class.
*/
Class User {
Public $ name;
Private $ passwd;
Protected $ email;
Public function _ construct (){
// Print _ CLASS __."";
$ This-> name = 'simple ';
$ This-> passwd = '123 ';
$ This-> email = 'bjbs _ 270@163.com ';
}
Public function show (){
Print "good ";
}
Public function inUserClassPublic (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}
Protected function inUserClassProtected (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}
Private function inUserClassPrivate (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}
}

Class simpleUser extends User {
Public function _ construct (){
// Print _ CLASS __."";
Parent: :__ construct ();
}

Public function show (){
Print $ this-> name. "// public ";
Print $ this-> passwd. "// private ";
Print $ this-> email. "// protected ";
}

Public function inSimpleUserClassPublic (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}

Protected function inSimpleUserClassProtected (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}

Private function inSimpleUserClassPrivate (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}
}

Class adminUser extends simpleUser {
Protected $ admin_user;
Public function _ construct (){
// Print _ CLASS __."";
Parent: :__ construct ();
}

Public function inAdminUserClassPublic (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}

Protected function inAdminUserClassProtected (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}

Private function inAdminUserClassPrivate (){
Print _ CLASS _. ':'. _ FUNCTION __."";
}
}

Class administrator extends adminUser {
Public function _ construct (){
Parent: :__ construct ();
}
}

/**
* In an instance of a class, only public attributes and methods can be called Through instantiation.
*/
$ S = new administrator ();
Print '-------------------';
$ S-> show ();
?>

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.