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

Source: Internet
Author: User
The difference between the public, protected, and private access control modes of php is protectedprivate. The differences between the public, protected, and private access control modes of php are as follows: the difference between the public, protected, and private access control modes of the public type in the subclass php is: protectedprivate
Differences between the public, protected, and private access control modes of php

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 ();
?>

Differences between the public, protected, and private access control modes of ingress php public: the public type is in the subclass...

Related Article

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.