inheritance
In the previous section, you learned about PHP classes, class methods, and class member variables. Now you’ll learn about inheritance in PHP classes. This section complements the previous one. (In fact, in order to keep a more casual post)
start
PHP inheritance inherits all attributes from the parent class, just as you inherit most of your father’s DNF. For example, Xiao Ming’s father has red hair, a black nose, blue eyes and a conical head. Let’s look at the following example to see how inheritance is implemented in PHP:
class Human {
public $sex;
public $hair;
private function printInfo($val){
echo 'The value passed in is'.$val.'<br/>';
}
public function set_Sex($val){
$this->printInfo($val);
$this->sex=$val;
}
public function set_Hair($val){
$this->hair=$val;
}
public function get_Sex(){
return $this->sex;
}
public function get_Hair(){
return $this->hair; }}class MutantHuman extends Human{}
$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');
echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();
? >
Copy the code
The above code example, modified from the previous section, creates a new class and inherits the Human class:
class MutantHuman extends Human{}
Copy the code
In the above class, the name of the class is MutantHuman, and the extends keyword, which means inherited from, is followed by the inherited class name Human. That is, Human is its father, and MutantHuman has its father’s methods and member variables. MutantHuman can use set_Sex and set_Hair and have sex and hair member variables. Therefore, set_Sex and set_Hair methods can be used after SuperMan is created in MutantHuman object:
$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');
echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();
Copy the code
The results are as follows:
Methods to rewrite
When a subclass integrates a method from its parent class, it can override that method, as shown in the following example:
class Human {
public $sex;
public $hair;
private function printInfo($val){
echo 'The value passed in is'.$val.'<br/>';
}
public function set_Sex($val){
$this->printInfo($val);
$this->sex=$val;
}
public function set_Hair($val){
$this->hair=$val;
}
public function get_Sex(){
return $this->sex;
}
public function get_Hair(){
return $this->hair; }}class MutantHuman extends Human{
public function get_Sex(){
echo 'This is in MutantHuman
';
return $this->sex; }}$SuperMan=new MutantHuman();
$SuperMan->set_Sex('nan');
$SuperMan->set_Hair('y');
echo $SuperMan->get_Sex();
echo ' ';
echo $SuperMan->get_Hair();
? >
Copy the code
In the example above, the subclass MutantHuman already has the methods of the parent class and can override the methods of the same name directly in the subclass to implement different features. After all, fathers and sons are different.
In the above example, subclass MutantHuman overrides the get_Sex method. Overwriting is rewriting a method whose function form is the same as that of its parent class, modifying the method to have different properties. In the method overridden above, the output is inEcho 'This is in MutantHuman <br/>';
. The results are as follows: