PHP OOP - Oorerwing
PHP - Wat is erfenis?
Oorerwing in OOP = Wanneer 'n klas van 'n ander klas afkomstig is.
Die kinderklas sal al die publieke en beskermde eiendomme en metodes van die ouerklas erf. Daarbenewens kan dit sy eie eienskappe en metodes hê.
'n Oorgeërfde klas word gedefinieer deur die extends
sleutelwoord te gebruik.
Kom ons kyk na 'n voorbeeld:
Voorbeeld
<?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();
?>
Voorbeeld Verduidelik
Die Strawberry-klas word van die Fruit-klas geërf.
Dit beteken dat die Strawberry-klas die publieke $name en $color eienskappe sowel as die publieke __construct() en intro() metodes van die Fruit klas kan gebruik as gevolg van oorerwing.
Die Strawberry-klas het ook sy eie metode: message().
PHP - Oorerwing en die Beskermde Toegangswysiger
In die vorige hoofstuk het ons geleer dat protected
toegang tot eienskappe of metodes binne die klas en deur klasse afgelei van daardie klas verkry kan word. Wat beteken dit?
Kom ons kyk na 'n voorbeeld:
Voorbeeld
<?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
?>
In die voorbeeld hierbo sien ons dat as ons probeer om 'n protected
metode (intro()) van buite die klas aan te roep, ons 'n fout sal ontvang. public
metodes sal goed werk!
Kom ons kyk na nog 'n voorbeeld:
Voorbeeld
<?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
?>
In die voorbeeld hierbo sien ons dat alles goed werk! Dit is omdat ons die
protected
metode (intro()) van binne die afgeleide klas noem.
PHP - Oorheersing van oorgeërfde metodes
Oorgeërfde metodes kan oorheers word deur die metodes (gebruik dieselfde naam) in die kinderklas te herdefinieer.
Kyk na die voorbeeld hieronder. Die __construct() en intro() metodes in die kinderklas (Strawberry) sal die __construct() en intro() metodes in die ouerklas (Fruit) ignoreer:
Voorbeeld
<?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 - Die finale sleutelwoord
Die final
sleutelwoord kan gebruik word om klasoorerwing te voorkom of om metode-oorheersing te voorkom.
Die volgende voorbeeld wys hoe om klasoorerwing te voorkom:
Voorbeeld
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
Die volgende voorbeeld wys hoe om metode-oortreding te voorkom:
Voorbeeld
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>