البرمجة الكائنية (OOP) في PHP - شرح مفصل
البرمجة الكائنية (OOP) في PHP - شرح مفصل
دليل متخصص من علاء عامر - مطور ومصمم مواقع وتطبيقات احترافية
البرمجة الكائنية (Object-Oriented Programming) هي نموذج برمجي قوي يجعل الكود أكثر تنظيماً وقابلية للصيانة. في PHP، تطورت OOP لتصبح الأساس في إطارات العمل الحديثة.
2️⃣ إنشاء الفئات الأساسية
<?php
class User {
// الخصائص (Properties)
private $name;
private $email;
protected $age;
public $isActive;
// الباني (Constructor)
public function __construct($name, $email, $age) {
$this->name = $name;
$this->email = $email;
$this->age = $age;
$this->isActive = true;
}
// الطرق (Methods)
public function getName() {
return $this->name;
}
public function setName($name) {
if (strlen($name) >= 2) {
$this->name = $name;
return true;
}
return false;
}
public function getInfo() {
return "الاسم: {$this->name}, البريد: {$this->email}";
}
// المدمر (Destructor)
public function __destruct() {
echo "تم حذف المستخدم: {$this->name}";
}
}
// إنشاء كائن
$user1 = new User("علاء عامر", "[email protected]", 30);
echo $user1->getInfo();
?>
4️⃣ الوراثة (Inheritance)
// الفئة الأساسية
class Vehicle {
protected $brand;
protected $model;
protected $year;
public function __construct($brand, $model, $year) {
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
}
public function getInfo() {
return "{$this->brand} {$this->model} ({$this->year})";
}
public function start() {
return "تم تشغيل المركبة";
}
}
// فئة وارثة
class Car extends Vehicle {
private $doors;
private $fuelType;
public function __construct($brand, $model, $year, $doors, $fuelType) {
parent::__construct($brand, $model, $year); // استدعاء باني الفئة الأب
$this->doors = $doors;
$this->fuelType = $fuelType;
}
// إعادة تعريف الطريقة (Method Overriding)
public function start() {
return "تم تشغيل السيارة بمفتاح الإشعال";
}
public function getCarDetails() {
return $this->getInfo() . " - {$this->doors} أبواب - {$this->fuelType}";
}
}
// فئة وارثة أخرى
class Motorcycle extends Vehicle {
private $engineSize;
public function __construct($brand, $model, $year, $engineSize) {
parent::__construct($brand, $model, $year);
$this->engineSize = $engineSize;
}
public function start() {
return "تم تشغيل الدراجة النارية بالكيك";
}
public function wheelie() {
return "عمل حركة استعراضية!";
}
}
// الاستخدام
$car = new Car("تويوتا", "كامري", 2023, 4, "بنزين");
$bike = new Motorcycle("هوندا", "CBR", 2022, "600cc");
echo $car->getCarDetails() . "\n";
echo $car->start() . "\n";
echo $bike->start() . "\n";
6️⃣ الفئات المجردة (Abstract Classes)
abstract class Shape {
protected $color;
public function __construct($color) {
$this->color = $color;
}
// طريقة عادية
public function getColor() {
return $this->color;
}
// طريقة مجردة - يجب تنفيذها في الفئات الوارثة
abstract public function calculateArea();
abstract public function draw();
}
class Circle extends Shape {
private $radius;
public function __construct($color, $radius) {
parent::__construct($color);
$this->radius = $radius;
}
public function calculateArea() {
return pi() * pow($this->radius, 2);
}
public function draw() {
return "رسم دائرة {$this->color} بنصف قطر {$this->radius}";
}
}
class Rectangle extends Shape {
private $width;
private $height;
public function __construct($color, $width, $height) {
parent::__construct($color);
$this->width = $width;
$this->height = $height;
}
public function calculateArea() {
return $this->width * $this->height;
}
public function draw() {
return "رسم مستطيل {$this->color} بأبعاد {$this->width}x{$this->height}";
}
}
💡 أفضل الممارسات في OOP
- اتبع مبدأ Single Responsibility: كل فئة لها غرض واحد فقط
- استخدم الوراثة بحكمة: فقط عند وجود علاقة "is-a"
- فضل التركيب على الوراثة: استخدم "has-a" بدلاً من "is-a" عند الإمكان
- اكتب كود قابل للاختبار: فصل المسؤوليات
- استخدم الواجهات: لضمان التوافق
الخطوة التالية
بعد إتقان OOP، انتقل إلى Design Patterns و SOLID Principles.
📩 تحتاج مساعدة في البرمجة الكائنية؟
قسم المقالة
البرمجة الكائنية (OOP) في PHP - شرح مفصل
دليل شامل للبرمجة الكائنية في PHP مع أمثلة عملية وتطبيقات حقيقية لفهم المفاهيم الأساسية.
التواصل والاستشارة
تواصل مباشر عبر الواتساب أو الهاتف لفهم احتياجات مشروعك بدقة.
التخطيط والجدولة
وضع خطة عمل واضحة مع جدول زمني محدد لكل مرحلة من المشروع.
البرمجة والتطوير
تطوير المشروع بأحدث التقنيات لضمان الأداء والأمان العاليين.
المراجعة والتسليم
ختبار شامل ومراجعة دقيقة قبل التسليم النهائي للمشروع.