Inheritance: Refers to a class as the parent class, another class can be its child class, the subclass inherits the property/method of the parent class to further add or modify
Grammar:
Child class extends parent class {
}
Note: Subclasses can inherit only one parent class
Subclass extends Pclass1,pclass2,pclass3{
}
This is not going to work.
1<?PHP2 classHuman {3 Private $height= 179;4 Public functioncry () {5 Echo' 5555<br/> ';6 }7 }8 classStuextendshuman{9 Ten } One $lisi=NewHuman (); A $lisi-cry (); - $lily=NewStu (); - $lily-cry (); the?>
In Public,protected,private
Subclasses can inherit the private of the parent class, but cannot be accessed directly, and can be accessed by calling the parent class's function in the parent class's function.
Subclasses can inherit the public and protected of the parent class and have access and modify permissions
1<?PHP2 classhuman{3 Private $wife= ' Little Sweet ';4 Public functionTell () {5 Echo $this->wife, ' <br/> ';6 }7 Public functioncry () {8 Echo' 5555<br/> ';9 }Ten } One classStuextendshuman{ A Public functionSubtell () { - Echo $this->wife, ' <br/> '; - } the } - $lisi=NewStu (); - $lisi->cry ();//5555 - $lisi->tell ();//Little Sweet . + $lisi->subtell ();//notice:undefined property:stu:: $wife, $this->wife in Subtell is a private property of the parent class and cannot be accessed directly
?>
1<?PHP2 classhuman{3 Private $wife= ' Little Sweet ';4 Public $age= 22;5 Public functioncry () {6 Echo' 5555<br/> ';7 }8 Public functionpshow () {9 Echo $this->wife, ' <br/> ';Ten } One } A classStuextendshuman{ - Private $wife= ' sister Lin '; - Public $height= 170; the Public functionSubtell () { - Echo $this->wife, ' <br/> '; - } - Public functionsshow () { +Parent::pshow (); - Echo $this->wife, ' <br/> '; + } A } at $lisi=NewStu (); - //Print_r ($lisi); - $lisi->sshow ();//Little Sweet, Sister Lin. Using Sshow to invoke the pshow of the parent class's public, and Pshow is within the parent class {}, you can call the private property $this->wife, if you delete the parent class $wife it will be an error, cannot access Private Property Stu:: $wife -?>
When a subclass inherits the properties/methods of the parent class, the permissions become more or less restrictive, such as the parent class's property/method is public, the subclass inherits only public, and cannot be protected/private
Inheritance of construction methods
Construction methods can also be inherited.
New subclass, if the constructor inherits from it, of course, it should be executed automatically.
But after inheriting it, subclasses rewrite the method of constructing a subclass that is naturally executed
(Note that the construction method of the parent class is no longer called)
The difference between private protected public three
Private protected public
Within this class Y Y y
Sub-class N Y y
Outside of class n n y
In Java, if the property/method is not written in front of any parameters that is private protected public is not written, it is also available, understood as friendly
In PHP, if private protected public are not written, it is understood as public, it is recommended to develop good habits, do not write
The Inheritance of PHP