وراثت (Inheritance)
«وراثت در PHP» یعنی یک کلاس از کلاس دیگر ویژگی می گیرد. «فرزند (Child)» از «والد (Parent)» می آید. سپس می تواند ویژگی ها و متدهای خودش را هم داشته باشد. کلمه «extends» این اتصال را می سازد.
تعریف وراثت در PHP
کلاس فرزند با extends ساخته می شود. سپس متدهای عمومی والد را می گیرد.
<?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 function message() {
echo "Am I a fruit or a berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
وراثت و protected
«protected» یعنی دسترسی فقط در همان کلاس و فرزندان. بیرون کلاس خطا می دهد.
<?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? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
نکته: فراخوانی protected از بیرون کلاس مجاز نیست. پس خطا طبیعی است.
اما می توانیم protected را داخل کلاس فرزند صدا بزنیم.
<?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? ";
$this->intro();
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
?>
بازنویسی متدها (Overriding)
در فرزند می توانیم متد هم نام را دوباره تعریف کنیم. سپس رفتار تغییر می کند.
<?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();
?>
final؛ جلوگیری از ارث بری و بازنویسی
final روی کلاس یعنی ارث بری ممنوع. روی متد یعنی بازنویسی ممنوع.
<?php
final class Fruit {
}
class Strawberry extends Fruit {
}
?>
<?php
class Fruit {
final public function intro() {
}
}
class Strawberry extends Fruit {
public function intro() {
}
}
?>
گام های عملی سریع
- با
extendsیک فرزند بساز. - متد عمومی را تست و صدا بزن.
- یک متد
protectedبساز و داخل فرزند صدا بزن. - یک متد را بازنویسی کن و تفاوت را ببین.
نکته: برای مرور مفاهیم، صفحه سطوح دسترسی و سازنده را هم ببین. همچنین لینک وراثت در PHP مرجع همین صفحه است.
جمع بندی سریع
- وراثت کد تکراری را کم می کند.
- protected بیرون کلاس خطا می دهد.
- بازنویسی رفتار جدید می سازد.
- final جلوی تغییرات ناخواسته را می گیرد.