TOC

This article is currently in the process of being translated into Persian (~37% done).

Classes:

Constructors and destructors

سازنده و ویرانگر توابع خاصی هستند که به صورت خودکار زمانی که یک شیء ایجاد یا ویران می شود فراخوانی می شوند. سازنده از بین این دو پرکاربردتر و مفیدتر است، خصوصا این که اجازه ارسال پارامترها را هنگام ساخت شیء فراهم می کند که با کمک این پارامترها متغییرها در شیء ایجاد شده مقداردهی اولیه می شوند. در زیر مثالی از یک کلاس با سازنده ای ساده ارائه شده است:

class Animal
{
    public $name = "No-name animal";
    
    public function __construct()
    {
        echo "I'm alive!";        
    }
}

همانطور که می بینید، سازنده درست شبیه به یک تابع معمولی با این تفاوت است که با دو آندرلاین (_) شروع می شود. در پی اچ پی، توابعی با دو آندرلاین قبل از نام تابع، معمولاً بیانگر تابعی خاص و جادویی است، تابعی با هدفی خاص و عملکردی فراتر از تابع معمولی. پس تابعی با نام "construct__"، تابع سازنده یک کلاس است و در زمان ساخت شیء به صورت خودکار فراخوانی می شود. بگذارید آن را امتحان کنیم:

$animal = new Animal();

With just that line of code, the object will be created, the constructor called and the lines of code in it executed, which will cause our "I'm alive!" line to be outputted. However, as mentioned previously, a big advantage of the constructor is the ability to pass parameters which can be used to initialize member variables. Let's try doing just that:

<?php
class Animal
{
    public $name = "No-name animal";
    
    public function __construct($name)
    {
        $this->name = $name;
    }
}

$animal = new Animal("Bob the Dog");
echo $animal->name;
?>

Declaring the constructor with parameters is just like declaring a regular function, and passing the parameter(s) is much like calling a regular function, but of course with the "new" keyword that we introduced earlier. A constructor can have as many parameters as you want.

Destructors

A destructor is called when the object is destroyed. In some programming languages, you have to manually dispose of objects you created, but in PHP, it's handled by the Garbage Collector, which keeps an eye on your objects and automatically destroys them when they are no longer needed. Have a look at the following example, which is an extended version of our previous example:

<?php
class Animal
{
    public $name = "No-name animal";
    
    public function __construct($name)
    {
        echo "I'm alive!";    
        $this->name = $name;
    }
    
    public function __destruct()
    {
        echo "I'm dead now :(";
    }
}

$animal = new Animal("Bob");
echo "Name of the animal: " . $animal->name;
?>

As you can see, the destructor is just like a constructor, only the name differs. If you try running this example, you will see first the constructor message, then the name of the animal that we manually output in the last line, and after that, the script ends, the object is destroyed, the destructor is called and the message about our poor animal being dead is outputted.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!