inheritance Using keywords:extends
PHP is single-inheritance, and there is only one parent class
The constructor of PHP can be inherited, but if the subclass also defines a constructor, the parent class is overwritten
Subclasses define the functions of the parent class, called overriding
The parent keyword, which is intended to be a parent, is currently the object of the parent class of the current class in the child class.
Use it to invoke the properties and behavior of the overridden parent class
Class animal{
Public $name;
Public $sex;
Public $age;
Public function __construct ($name)
{
$this->name = $name;
}
Public function shout () {
echo ' Animals all have their own cries ';
}
Public Function desc () {
}
}
Class Dog extends animal{
Public function __construct ($name)
// {
$this->name = $name;
// }
/**
* Subclasses define the functions of the parent class, called Overrides
*/
Public function shout () {
Echo ', Dog's cry: Wang ~ ~ Wang ~ Wang ~;
}
Public Function desc () {
Echo ' Dog's name: '. $this->name;
parent::shout ();
$this->shout ();
}
}
Class Cat extends animal{
Public function __construct ($name)
// {
$this->name = $name;
// }
Public function shout () {
Echo ', the cat's cry: Meow ~ ~ Meow ~ ~ ~;
}
Public Function desc () {
Echo ' Meow's name: '. $this->name;
$this->shout ();
}
}
$animal = new Dog (' blessings ');
$animal->desc ();
Echo ' <br><br> ';
$animal = new Cat (' Mimi ');
$animal->desc ();
php--inheritance