PHP OOP - Dědičnost
PHP – Co je dědičnost?
Dědičnost v OOP = Když třída pochází z jiné třídy.
Podřízená třída zdědí všechny veřejné a chráněné vlastnosti a metody od nadřazené třídy. Navíc může mít své vlastní vlastnosti a metody.
Zděděná třída je definována pomocí extends
klíčového slova.
Podívejme se na příklad:
Příklad
<?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();
?>
Příklad vysvětlen
Třída Jahoda je zděděna od třídy Fruit.
To znamená, že třída Strawberry může používat veřejné vlastnosti $name a $color a také veřejné metody __construct() a intro() ze třídy Fruit kvůli dědičnosti.
Třída Strawberry má také svou vlastní metodu: message().
PHP – dědičnost a modifikátor chráněného přístupu
V předchozí kapitole jsme se dozvěděli, že k protected
vlastnostem nebo metodám lze přistupovat v rámci třídy a třídami odvozenými z této třídy. Co to znamená?
Podívejme se na příklad:
Příklad
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");
// OK. __construct() is public
$strawberry->message(); // OK. message()
is public
$strawberry->intro(); // ERROR. intro()
is protected
?>
Ve výše uvedeném příkladu vidíme, že pokud se pokusíme zavolat protected
metodu (intro()) mimo třídu, obdržíme chybu. public
metody budou fungovat dobře!
Podívejme se na další příklad:
Příklad
<?php
class Fruit {
public $name;
public
$color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The
fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
// Call protected
method from within derived class - OK
$this ->
intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is
public
$strawberry->message(); // OK. message() is
public and it calls intro() (which is protected) from within the
derived class
?>
Ve výše uvedeném příkladu vidíme, že vše funguje dobře! Je to proto, že metodu (intro()) voláme
protected
zevnitř odvozené třídy.
PHP - Přepsání zděděných metod
Zděděné metody lze přepsat předefinováním metod (použijte stejný název) v podřízené třídě.
Podívejte se na příklad níže. Metody __construct() a intro() v podřízené třídě (Strawberry) přepíší metody __construct() a intro() v nadřazené třídě (Fruit):
Příklad
<?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}.";
}
}
class
Strawberry extends Fruit {
public $weight;
public
function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color},
and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red",
50);
$strawberry->intro();
?>
PHP – konečné klíčové slovo
Klíčové final
slovo lze použít k zabránění dědičnosti třídy nebo k zabránění přepsání metody.
Následující příklad ukazuje, jak zabránit dědění třídy:
Příklad
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
Následující příklad ukazuje, jak zabránit přepsání metody:
Příklad
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>