PHP OOP — Dziedziczenie
PHP — co to jest dziedziczenie?
Dziedziczenie w OOP = Kiedy klasa pochodzi z innej klasy.
Klasa potomna odziedziczy wszystkie publiczne i chronione właściwości i metody z klasy nadrzędnej. Ponadto może mieć własne właściwości i metody.
Odziedziczoną klasę definiuje się za pomocą extends
słowa kluczowego.
Spójrzmy na przykład:
Przykład
<?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();
?>
Przykład wyjaśniony
Klasa Strawberry jest dziedziczona z klasy Fruit.
Oznacza to, że klasa Strawberry może używać publicznych właściwości $name i $color, a także publicznych metod __construct() i intro() z klasy Fruit z powodu dziedziczenia.
Klasa Strawberry ma również własną metodę: message().
PHP — dziedziczenie i modyfikator dostępu chronionego
W poprzednim rozdziale dowiedzieliśmy się, że protected
dostęp do właściwości lub metod można uzyskać w obrębie klasy oraz przez klasy pochodzące z tej klasy. Co to znaczy?
Spójrzmy na przykład:
Przykład
<?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
?>
W powyższym przykładzie widzimy, że jeśli spróbujemy wywołać protected
metodę (intro()) spoza klasy, otrzymamy błąd. public
metody będą działać dobrze!
Spójrzmy na inny przykład:
Przykład
<?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
?>
W powyższym przykładzie widzimy, że wszystko działa dobrze! Dzieje się tak, ponieważ wywołujemy
protected
metodę (intro()) z wnętrza klasy pochodnej.
PHP — zastępowanie metod dziedziczonych
Metody dziedziczone można przesłonić, przedefiniowując metody (użyj tej samej nazwy) w klasie podrzędnej.
Spójrz na poniższy przykład. Metody __construct() i intro() w klasie potomnej (Strawberry) zastąpią metody __construct() i intro() w klasie nadrzędnej (Fruit):
Przykład
<?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 - ostatnie słowo kluczowe
Słowo final
kluczowe może służyć do zapobiegania dziedziczeniu klas lub zastępowaniu metod.
Poniższy przykład pokazuje, jak zapobiec dziedziczeniu klas:
Przykład
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
Poniższy przykład pokazuje, jak zapobiec zastępowaniu metody:
Przykład
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>