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