Object-Oriented PHP الدرس السابع
Constructors & Destructor
السلام عليكم ورحمة الله وبركاته
مرحبا بكم في مدونة مايندز في الدرس السادس من اساسيات البرمجة الموجهة.
Constructors & Destructorهي دوال تعمل تلقائيا مع بداية عمل الـ Object ونهايه العمليات فيه
Constructors:
تستخدم عند الحاجة لتشغيل دالة تلقائيا بمجرد إنشاء الـ Object . يمكن ان تتم هذه العملية يدويا لكن في بعض الحالات نفضل ان تكون
أتوماتيكية .
- يمكن ان يتقبل الـ constructor البراميترات كما في الـMethods
- كما يمكنه استدعاء الدوال من الكلاس نفسه او الكلاسات الاخرى ايضا الدوال العادية التي لا يحتويها كلاس محدد
- يمكن ايضا ان يستدعي الـ constructors الاخرى بما فيها الـ constructors التي في الكلاسات الـ Parents
الصيغة العامة للـ constructors هي
كالتالي:
Function __construct(algument1, algument2, algument3, algument4){
// initialization code
}
<?php
Class Book{
private $title;
private $isbn;
private $copies;
Function __construct($isbn){
$this->setIsbn($isbn);
$this-> getTitle ();
$this-> getNuberCopies ();
}
public function setIsbn($isbn){
$this->isbn = $isbn;
}
public function getTitle(){
$this->title = 'Book title';
print "Title:{$this->title}<br />";
}
public function getNuberCopies(){
$this->copies= 5;
print "Number of copies available :{$this->copies}<br />";
}
}
$book = new Book("0615303889");
?>
Result:
Title :
Book title
Number of
copies available : 5
وهذا المثال يوضح لك التالي:
- ان الـ constructors تعمل تلقائيا
- ان الـ constructors تشغل الدوال
- ان الـ constructors تتقبل البراميترات
ويمكنك تضمين دوال اعقد او ابسط حسب التطبيق
Invoking Parent constructors
يمكن أيضا ان نستدعي او نشغل constructor الكلاس الاب في حالة الوراثة باستخدام الكلمة parent كالتالي:
<?php
Class Employee{
protected $name;
protected $title;
function __construct(){
echo "<p>Employee constructor Called !</p>";
}
}
Class Manager extends Employee{
Function __ construct(){
parent:: __construct();
echo "<p> Manager constructor Called !</p>";
}
}
$employee = new Employee();
?>
Result :
Employee
constructor Called !
Manager constructor
Called !
إذا حذفنا السطر
parent:: __construct();
Result :
Manager constructor
Called !
Invoking Unrelated Constructors
تشغيل Constructors من كلاسات أخري ليست بينها علاقات وراثة.. تكون الصيغة نفس السابقة (صيفة تشغيل Constructor للكلاس الأب) لكن بدلا من parent نكتب اسم الكلاس.
Classname::__construct();
E.G.
Employee::__construct();
Manager::__construct();
Destructors
وهي عكس الـ Constructorsفهي تعمل
بعد إنتهاء عمل الكلاس يعني بع تنفيذ أخر سطر من الكود تنفذ الـ Destructors.
<?php
Class Book{
private $title;
private $isbn;
private $copies;
Function __construct(){
Echo "<p> Book class instance Is Created !</p>";
}
Function __construct(){
Echo "<p> Book class instance Is Destroyed !</p>";
}
Public function testing(){
Echo "<p> Testing Constructors & destructor ordering !</p>";
}
}
$book = new Book();
$book-> testing();
?>
Result:
Book class
instance Is Created !
Testing Constructors & Destructors ordering !
Book class
instance Is Destroyed!
إذا وضعت اي دالة وشغلتها في هذا الكلاس فإن الـ Constructors يعمل أولا والـ Destructors يعمل اخر واحد.
إلى هذه النقطة نكون قد وصلنا إلى نهاية درس اليوم موعدنا يوم السبت والثلاثاء من كل الاسبوع ان شاء الله
0 comments :
إرسال تعليق