A difficult question about PHP inheritance! Class B
{
Public $ name = "aaa ";
Function fb ()
{
Echo get_class ($ this ),"
";
Echo $ this-> name ,"
";
}
}
Class Eb extends B
{
Public $ name = "xxxxxxxxxxxxxxsylar ";
Function _ construct ()
{
Parent: fb ();
}
}
$ Eb = new Eb ();
// When $ name in the parent class and subclass are declared as public, the Eb constructor is executed and the output result is the value of $ name in the subclass xxxxxxxxxxxxxxxxsylar.
// When the $ name in the parent class and subclass are declared as private, the Eb constructor is executed and the output result is the value aaa of $ name in the subclass.
Solving?
Reply to discussion (solution)
When $ name is declared as public, the child class naturally overwrites the parent class.
This should not be a problem, right?
When $ name is declared as private, because Method fb is a parent class, you can only access the private attributes of the parent class.
Copy the fb method to Eb and add $ this-> fb () to _ construct ();
The truth is
This is called overload.
This is because the public subclass inherits the variable name from the parent class and loads it, overwrites the original value,
When it is private, the subclass cannot overwrite the variables of the parent class. at this time, only the original values of the parent class can be output.
1. Private attributes cannot be overwritten,
2. the subclass calls the private attribute of the parent class and directly outputs the parent class!