I hope you can help me with some questions about inheritance! Class & nbsp; f & nbsp ;{& nbsp; & nbsp; private & nbsp ;$ str & nbsp; 123; & nbsp; & nbsp; public & nbsp; function & nbsp; fun () & nbsp ;{& nbsp; & nbsp regarding inheritance, hope you can help me!
Class f {
Private $ str = "123 ";
Public function fun (){
Echo $ this-> str;
}
}
Class s extends f {
Public $ str = "abc ";
}
$ S = new s ();
$ S-> fun ();
Output: 123
Class f {
Public $ str = "123 ";
Public function fun (){
Echo $ this-> str;
}
}
Class s extends f {
Public $ str = "abc ";
}
$ S = new s ();
$ S-> fun ();
Output: abc
The first output is confused. Why does the output the attribute value of the parent class? Please analyze and analyze the memory for me. it is better to see the figure below. thank you.
Share: More
------ Solution --------------------
Private attributes are not inherited
Private attributes can only be accessed using methods that define their classes.
------ Solution --------------------
Baidu just now, I understand, Hey, 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.