A difficult question about PHP inheritance! & Nbsp; class & nbsp; B & nbsp; & nbsp; {& nbsp; 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? PHP, inheriting class
------ 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
------ Solution --------------------
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.
------ Solution --------------------
1. Private attributes cannot be overwritten,
2. the subclass calls the private attribute of the parent class and directly outputs the parent class!
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.