PHP OOP - الوراثة
PHP - ما هو الميراث؟
الوراثة في OOP = عندما تشتق فئة من فئة أخرى.
سترث الفئة الفرعية جميع الخصائص والأساليب العامة والمحمية من الفئة الرئيسية. بالإضافة إلى ذلك ، يمكن أن يكون لها خصائصها وطرقها الخاصة.
يتم تحديد فئة موروثة باستخدام 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}.";
}
}
// 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();
?>
شرح المثال
فئة الفراولة موروثة من فئة الفاكهة.
هذا يعني أن فئة Strawberry يمكنها استخدام اسم $ العام وخصائص اللون $ بالإضافة إلى أساليب __construct () و intro () العامة من فئة Fruit بسبب الوراثة.
صنف Strawberry له أيضًا طريقته الخاصة: message ().
PHP - الوراثة ومعدل الوصول المحمي
لقد تعلمنا في الفصل السابق أنه 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? ";
}
}
// 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
طريقة (مقدمة ()) من خارج الفصل ، فسنحصل على خطأ. public
الأساليب ستعمل بشكل جيد!
لنلق نظرة على مثال آخر:
مثال
<?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
?>
في المثال أعلاه نرى أن كل شيء يعمل بشكل جيد! ذلك لأننا نطلق على
protected
الطريقة (intro ()) من داخل الصنف المشتق.
PHP - تجاوز الطرق الموروثة
يمكن تجاوز الطرق الموروثة عن طريق إعادة تعريف الطرق (استخدم نفس الاسم) في الفئة الفرعية.
انظر للمثال ادناه. ستتجاوز أساليب __construct () و intro () في الفئة الفرعية (Strawberry) أساليب __construct () و intro () في الفئة الأصلية (Fruit):
مثال
<?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 - الكلمة الرئيسية النهائية
يمكن final
استخدام الكلمة الأساسية لمنع وراثة الطبقة أو لمنع تجاوز الطريقة.
يوضح المثال التالي كيفية منع وراثة الفئات:
مثال
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
يوضح المثال التالي كيفية منع تجاوز الطريقة:
مثال
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>