PHP Inheritance
Object-Oriented Programming рдорд╛ Inheritance (рдЙрддреНрддрд░рд╛рдзрд┐рдХрд╛рд░) рдПрдЙрдЯрд╛ рдорд╣рддреНрддреНрд╡рдкреВрд░реНрдг concept рд╣реЛред
рдЬрдм рдПрдЙрдЯрд╛ class рдЕрд░реНрдХреЛ class рдмрд╛рдЯ derive рд╣реБрдиреНрдЫ, child class рд▓реЗ parent class рдХрд╛ рд╕рдмреИ public рд░ protected properties рд░ methods рдкрд╛рдЙрдБрдЫ (inherit рдЧрд░реНрдЫ)ред рдпрд╕рдХрд╛ рдЕрд▓рд╛рд╡рд╛, child class рдХреЛ рдЖрдлреНрдиреИ properties рд░ methods рдкрдирд┐ рд╣реБрди рд╕рдХреНрдЫрдиреНред
Inherited class рд▓рд╛рдИ extends keyword рдкреНрд░рдпреЛрдЧ рдЧрд░реЗрд░ define рдЧрд░рд┐рдиреНрдЫред
Example
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>