PHP OOP - Kalıtım
PHP - Kalıtım Nedir?
OOP'de Kalıtım = Bir sınıf başka bir sınıftan türetildiğinde.
Alt sınıf, tüm genel ve korumalı özellikleri ve yöntemleri üst sınıftan devralır. Ayrıca kendine has özellikleri ve yöntemleri olabilir.
Miras alınan bir sınıf, anahtar kelime kullanılarak tanımlanır extends
.
Bir örneğe bakalım:
Örnek
<?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();
?>
Örnek Açıklama
Strawberry sınıfı, Fruit sınıfından miras alınır.
Bu, Strawberry sınıfının kalıtım nedeniyle Fruit sınıfından public $name ve $color özelliklerinin yanı sıra public __construct() ve intro() yöntemlerini kullanabileceği anlamına gelir.
Strawberry sınıfının da kendi yöntemi vardır: message().
PHP - Kalıtım ve Korumalı Erişim Değiştirici
protected
Bir önceki bölümde, özelliklere veya yöntemlere sınıf içinde ve o sınıftan türetilen sınıflar tarafından erişilebileceğini öğrendik . Bu ne anlama geliyor?
Bir örneğe bakalım:
Örnek
<?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
?>
protected
Yukarıdaki örnekte sınıf dışından bir method (intro()) çağırmaya çalışırsak hata alacağımızı görüyoruz. public
yöntemler işe yarayacak!
Başka bir örneğe bakalım:
Örnek
<?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
?>
Yukarıdaki örnekte her şeyin yolunda gittiğini görüyoruz! Bunun nedeni,
protected
yöntemi (intro()) türetilmiş sınıfın içinden çağırmamızdır.
PHP - Devralınan Yöntemleri Geçersiz Kılma
Miras alınan yöntemler, alt sınıfta yöntemler yeniden tanımlanarak (aynı adı kullanarak) geçersiz kılınabilir.
Aşağıdaki örneğe bakın. Alt sınıftaki (Strawberry) __construct() ve intro() yöntemleri, üst sınıftaki (Fruit) __construct() ve intro() yöntemlerini geçersiz kılar:
Örnek
<?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 - Son Anahtar Kelime
Anahtar final
kelime, sınıf mirasını önlemek veya yöntemin geçersiz kılınmasını önlemek için kullanılabilir.
Aşağıdaki örnek, sınıf mirasının nasıl önleneceğini gösterir:
Örnek
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
Aşağıdaki örnek, yöntemi geçersiz kılmanın nasıl önleneceğini gösterir:
Örnek
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>