PHP OOP - Ereditarietà
PHP - Che cos'è l'ereditarietà?
Ereditarietà in OOP = Quando una classe deriva da un'altra classe.
La classe figlia erediterà tutte le proprietà ei metodi pubblici e protetti dalla classe padre. Inoltre, può avere proprietà e metodi propri.
Una classe ereditata viene definita utilizzando la extends
parola chiave.
Diamo un'occhiata a un esempio:
Esempio
<?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();
?>
Esempio spiegato
La classe Fragola è ereditata dalla classe Frutta.
Ciò significa che la classe Strawberry può utilizzare le proprietà public $name e $color così come i metodi public __construct() e intro() della classe Fruit a causa dell'ereditarietà.
Anche la classe Strawberry ha il suo metodo: message().
PHP - Ereditarietà e modificatore di accesso protetto
Nel capitolo precedente abbiamo appreso che protected
è possibile accedere a proprietà o metodi all'interno della classe e dalle classi derivate da quella classe. Che cosa significa?
Diamo un'occhiata a un esempio:
Esempio
<?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
?>
Nell'esempio sopra vediamo che se proviamo a chiamare un protected
metodo (intro()) dall'esterno della classe, riceveremo un errore. public
i metodi funzioneranno bene!
Diamo un'occhiata a un altro esempio:
Esempio
<?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
?>
Nell'esempio sopra vediamo che tutto funziona bene! È perché chiamiamo il
protected
metodo (intro()) dall'interno della classe derivata.
PHP - Sovrascrivere metodi ereditati
I metodi ereditati possono essere sovrascritti ridefinendo i metodi (usando lo stesso nome) nella classe figlia.
Guarda l'esempio qui sotto. I metodi __construct() e intro() nella classe figlia (Strawberry) sovrascriveranno i metodi __construct() e intro() nella classe genitore (Fruit):
Esempio
<?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 - La parola chiave finale
La final
parola chiave può essere utilizzata per impedire l'ereditarietà della classe o per impedire l'override del metodo.
L'esempio seguente mostra come impedire l'ereditarietà delle classi:
Esempio
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
L'esempio seguente mostra come impedire l'override del metodo:
Esempio
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>