Analysis of private property inheritance issues in php classes. Take a closer look at this sentence if the parent class has private attributes. The method of the parent class serves only the private attributes of the parent class. The following is a series of columns to deepen understanding. this example looks odd. please read this sentence carefully if the parent class has private attributes. The method of the parent class serves only the private attributes of the parent class.
The following describes how to use a series of columns.
This example looks strange. a new attribute $ sal is defined in the subclass, but the system returns the attribute of the parent class.
The code is as follows:
Class employee {
Private $ sal = 3000;
// Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();
?>
Program running result:
The code is as follows:
PHP 5.3.8
3000
Parent's $ sal 3000
If the attribute quilt class in the parent class is overwritten. The result is as follows. Note that the attribute definition of row 3 is changed to protected.
The code is as follows:
Class employee {
// Private $ sal = 3000;
Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();
?>
Program running result:
The code is as follows:
PHP 5.3.8
5000
Parent's $ sal 5000
The private $ sal of the parent class in the first column is not overwritten, so $ manager-> getSal () the method of this parent class calls the private attribute of the parent class $ sal. at this time, there are two $ sal in the memory.
Protected $ sal of the parent class in the second column is overwritten $ manager-> getSal () the method call of this parent class has been overwritten $ sal of the parent class does not exist in the memory. at this time, there is only one $ sal in the memory.
Next, let's look at the third column.
The method to override a subclass is valid for the current private.
The code is as follows:
Class employee {
Private $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Private $ sal = 5000;
// Rewrite method
Public function getSal (){
Return $ this-> sal;
}
Public function getParentSal (){
// Here the private attribute of the parent class is returned.
Return parent: getSal ();
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
Echo"
";
Echo "parent's \ $ sal". $ manager-> getParentSal ();
?>
Running result
The code is as follows:
PHP 5.3.8
5000
Parent's $ sal 3000
The subclass in this column overrides the getSal () method, so it calls the attributes of the subclass.
If you comment out this line of the subclass
// Private $ sal = 5000;
You will find an error: Notice: Undefined property: Manager ::$ sal in E: \ wamp \ www \ oo \ 2-5 \ 2-5-3.php on line 14
If you comment out 12 rows of subclass rewriting methods, echo $ manager-> getSal (); the result is the private attribute of the parent class $ sal 3000.
Open the zend debugging status to check the memory. Note that there are two $ sal at the bottom. 3000 and 5000 respectively.
The code is as follows:
Class employee {
Private $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
Return $ this-> sal;
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
?>
Program running result:
The code is as follows:
PHP 5.3.8
3000
Change the attribute $ sal of the parent class to protected, and the subclass overrides the attribute of the parent class. There is only one $ sal in the memory.
The code is as follows:
Class employee {
Protected $ sal = 3000;
Public function getSal (){
Return $ this-> sal;
}
}
Class Manager extends employee {
Protected $ sal = 5000;
Public function getParentSal (){
Return $ this-> sal;
}
}
$ Manager = new Manager ();
Echo "PHP". phpversion ()."
";
Echo $ manager-> getSal ();
?>
Program running result:
The code is as follows:
PHP 5.3.8
5000
If you have learned java, you will find it hard to understand.
When a subclass is created in Java, attributes and methods of the parent class are created in the memory, and even constructors are called.
PHP5 does not. PHP5 uses parent: instead of parent-> to call the parent class, which means PHP5 does not want to create the parent class in the memory. PHP5 wants to make inheritance easier than Java.
Just adapt.
If the parent class has private attributes. The method of the parent class serves only the private attributes of the parent class. The following is a series of columns to deepen understanding. this example looks odd...