PHP Class Constructor and Destructor

Spread the love

PHP Class Constructor and Destructor

In the previous tutorial, we have seen the use of $this variable to point out the current calling object of a class. In this tutorial, you will see what are PHP Class Constructor and Destructor methods and their working in detail.

There are many magic methods available in Object oriented PHP. Two of them are __construct used to create a constructor and __destruct to create a destructor in PHP OOP.

What is a PHP Class constructor?

The PHP Class constructor is a method of more specifically a magic method which gets called automatically whenever an object gets created.

The constructor method is created using an OOP PHP magic method __construct(). __construct() method will be called at the first as soon as the object of a class gets created by using a new keyword.

Let’s see an example:


class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;

    public function __construct()
    {
        echo "An object of a class Mobile is created.<br>";
    }

    public function calling()
    {
        return "Calling from a ". $this->companyName." mobile, with ".$this->color." color.";
    }
}

$iphone = new Mobile();



The output of the above example will be:


An object of a class Mobile is created.

As you see, as soon as a new object $iphone is created the constructor method __construct called automatically.

Use of a constructor

You can use a constructor method to initialize certain properties or calling other methods from inside constructor which you wants should get executed as soon as the new object gets created.

A PHP Class Constructor can be used for initialization purpose in your class.

A PHP class constructor with parameters

You can also pass parameters to the constructor and use them inside a constructor.


For example:


<?php
class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;

    public function __construct($name, $color)
    {
        $this->companyName = $name;
        $this->color = $color;
        echo "An object of a class Mobile is created with company name $this->companyName and a color $this->color.";
    }

    public function calling()
    {
        return "Calling from a ". $this->companyName." mobile, with ".$this->color." color.";
    }
}

$iphone = new Mobile("Apple", "Black"); //Passing arguments to the constructor

?>

The output will be:


An object of a class Mobile is created with company name Apple and a color Black.

Here we have sent few arguments while creating a new object $iphone, inside circular braces i.e “Apple” and “Black“. And catch these arguments in __construct($name, $color).

If you declare a constructor with parameters but do not send any parameters while creating an object then a fatal error will be displayed at the time of creating an object.

For more information on the constructor, you can also check the PHP official documentation.

What is a destructor?

A destructor is also a magic method which will be called automatically when the working of an object gets completed.

The destructor will be created using a magic method __destruct(). The destructor method clears all the resources allocated to the object once an object is destroyed.

Let’s add __destruct() method in our class and create an object.

<?php
class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;

    public function __construct($name, $color)
    {
        $this->companyName = $name;
        $this->color = $color;
        echo "An object of a class Mobile is created with company name $this->companyName and a color $this->color.";
    }

    public function calling()
    {
        return "<br>Calling from ". $this->companyName." mobile, with ".$this->color." color.";
    }

    public function __destruct()
    {
        echo "<br>The destructor method is called.";
    }
}

$iphone = new Mobile("Apple", "Black");
echo $iphone->calling();

?>



The output will be:


An object of a class Mobile is created with company name Apple and a color Black.
Calling from Apple mobile, with Black color.
The destructor method is called.

In the above example, a __destruct() method is called automatically.

You can also call destructor function manually using unset() on an object. Look into the below example.

<?php
class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;

    public function __construct($name, $color)
    {
        $this->companyName = $name;
        $this->color = $color;
        echo "An object of a class Mobile is created with company name $this->companyName and a color $this->color.";
    }

    public function calling()
    {
        return "<br>Calling from a ". $this->companyName." mobile, with ".$this->color." color.";
    }

    public function __destruct()
    {
        echo "<br>The destructor method is called.";
    }
}

$iphone = new Mobile("Apple", "Black");
unset($iphone);
echo $iphone->calling();

?>



The output will be:


An object of a class Mobile is created with company name Apple and a color Black.
The destructor method is called.

Along with the above output, you will also see the Notice as an Undefined variable: iPhone and a fatal error for calling() method execution as because an object is got destructed calling() method becomes unreachable.

For more information on the destructor, you can also check the PHP official documentation.

I hope you have got a clear picture above PHP Class Constructor and destructors in this tutorial.

Please comment if you have any difficulty, will try to resolve it as soon as possible.

In the next tutorial you will learn about public and private access modifiers in object oriented PHP.


Spread the love

Leave a Comment