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...