Inheritance in PHP
Inheritance in PHP
قائمة المصطلحات والتعريفات:
المقدمة:
في البرمجة الكائنية (OOP)، تعتبر الوراثة أحد أهم المفاهيم التي تساعد على إعادة استخدام الشيفرة وتوسيعها بسهولة. تسمح الوراثة بإنشاء فئات جديدة تعتمد على فئات موجودة
تهدف هذه الوثيقة إلى شرح مفهوم الوراثة في لغة البرمجة PHP بشكل شامل ومبسط، مع تقديم أمثلة حقيقية لتمكين المطورين من فهم كيفية استخدام هذا المفهوم في التطبيقات البرمجية.
ما هي الوراثة؟
الوراثة هي عملية يمكن من خلالها لفئة فرعية (Child Class) أن تكتسب خصائص وطرق فئة رئيسية (Parent Class). هذا يسمح بإعادة استخدام الكود الموجود وإضافة وظائف جديدة أو تعديلها حسب الحاجة.
كيفية استخدام الوراثة في PHP
يتم استخدام الكلمة المحجوزة extends للإشارة إلى أن الفئة الفرعية ترث من الفئة الرئيسية.
<?php
// الأب
class Vehicle {
public $brand;
public $color;
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
public function display() {
echo "This is a " . $this->color . " " . $this->brand . ".";
}
}
// الابن
class Car extends Vehicle {
public $model;
public function __construct($brand, $color, $model) {
parent::__construct($brand, $color);
$this->model = $model;
}
public function display() {
parent::display();
echo " Model: " . $this->model . ".";
}
}
$myCar = new Car("BMW", "red", "M7");
$myCar->display(); // This is a red BMW. Model: M7.
?>
الوراثة المتعددة (Multiple Inheritance) والواجهات (Interfaces)
لا تدعم الوراثة المتعددة بشكل مباشر، ولكن يمكن تحقيقها باستخدام الواجهات (Interfaces) والسمات (Traits).
استخدام الواجهات (Interfaces)
<?php
interface Engine {
public function startEngine();
}
interface Wheels {
public function rotateWheels();
}
class Car implements Engine, Wheels {
public function startEngine() {
echo "Engine started.";
}
public function rotateWheels() {
echo "Wheels rotating.";
}
}
$myCar = new Car();
$myCar->startEngine();
$myCar->rotateWheels();
?>
استخدام السمات (Traits)
<?php
trait GPS {
public function useGPS() {
echo "Using GPS.";
}
}
class Car {
use GPS;
public function drive() {
echo "Driving the car.";
}
}
$myCar = new Car();
$myCar->drive();
$myCar->useGPS();
?>
الوراثة العميقة (Deep Inheritance) والمشكلات المحتملة
الوراثة العميقة تعني وجود عدة مستويات من الوراثة، حيث ترث فئة من فئة أخرى والتي هي ترث من فئة ثالثة وهكذا. يمكن أن تؤدي الوراثة العميقة إلى تعقيد الكود وصعوبة تتبع الأخطاء.
<?php
class A {
public function printMe() {
echo "Class A";
}
}
class B extends A {
public function printMe() {
parent::printMe();
echo "Class B";
}
}
class C extends B {
public function printMe() {
parent::printMe();
echo "Class C";
}
}
$obj = new C();
$obj->printMe(); // Class AClass BClass C
?>
أمثلة:
نظام إدارة المستخدمين
<?php
class User {
protected $name;
protected $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getDetails() {
return $this->name . " (" . $this->email . ")";
}
}
class Admin extends User {
private $level;
public function __construct($name, $email, $level) {
parent::__construct($name, $email);
$this->level = $level;
}
public function getDetails() {
return parent::getDetails() . " - Level: " . $this->level;
}
}
$admin = new Admin("Muhmad", "Muhmad@example.com", 5);
echo $admin->getDetails(); // Muhmad (Muhmad@example.com) - Level: 5
?>
المراجع:
ليست هناك تعليقات: