PHP 5 Constructor and Destructor

When we create an object of any class, we need to set properties of that object before using it. We can do that by first initialising the object and then setting values for the properties, either by using the -> operator if the variables are public, or using the public setter methods for the private variables.

To create and initialize a class object in a single step, PHP provides a special method called as Constructor, which is used to construct the object by assigning the required property values while creating the object.

And for destroying the object Destructor method is used.


Syntax for defining Constructor and Destructor

In PHP, we have special functions to define constructor and destructor for a class, they are: __construct() and __destruct().

<?php
    class <CLASS_NAME> {
        
        // constructor
        function __construct() {
            // initialize the object properties
        }
        
        // destructor
        function __destruct() {
            // clearing the object reference
        }
    }
?>

Constructor can accept arguments, whereas destructors won't have any argument because a destructor's job is to detroy the current object reference.


PHP Constructor

Let's take the example of a class Person which has two properties, fname and lname, for this class we will define a constructor for initialising the class properties(variables) at the time of object creation.

<?php
    class Person {
        // first name of person
        private $fname;
        // last name of person
        private $lname;
        
        // Constructor
        public function __construct($fname, $lname) {
            echo "Initialising the object...<br/>"; 
            $this->fname = $fname;
            $this->lname = $lname;
        }
        
        // public method to show name
        public function showName() {
            echo "My name is: " . $this->fname . " " . $this->lname; 
        }
    }
    
    // creating class object
    $john = new Person("John", "Wick");
    $john->showName();
    
?>

Initialising the object... My name is John Wick

While earlier, we were using the -> operator to set value for the variables or used the setter methods, in case of a constructor method, we can assign values to the variables at the time of object creation.

If a class has a constructor then whenever an object of that class is created, the constructor is called.


PHP Constructor Overloading

PHP doesn't support function overloading hence we cannot have multiple implementations for constructor in a class.


PHP Destructor

PHP Destructor method is called just before PHP is about to release any object from its memory. Generally, you can close files, clean up resources etc in the destructor method. Let's take an example,

<?php
    class Person {
        // first name of person
        private $fname;
        // last name of person
        private $lname;
        
        // Constructor
        public function __construct($fname, $lname) {
            echo "Initialising the object...<br/>"; 
            $this->fname = $fname;
            $this->lname = $lname;
        }
        
        // Destructor
        public function __destruct(){
            // clean up resources or do something else
            echo "Destroying Object...";
        }
        
        // public method to show name
        public function showName() {
            echo "My name is: " . $this->fname . " " . $this->lname . "<br/>"; 
        }
    }
    
    // creating class object
    $john = new Person("John", "Wick");
    $john->showName();
    
?>

Initialising the object... My name is: John Wick Destroying Object...

As we can see in the output above, as the PHP program ends, just before it PHP initiates the release of the object created, and hence the destructor method is called.

The destructor method cannot accept any argument and is called just before the object is deleted, which happens either when no reference exist for an object or when the PHP script finishes its execution.

NOTE: If you see a PHP class with a function having same name as the name of the class, then that function will act as the constructor. In older versions of PHP, constructor was not defined using the __construct() name, but it used to have the same name as the class name just like Core Java.

For example:

<?php
    class Person {
        // first name of person
        private $fname;
        // last name of person
        private $lname;
        
        // Constructor
        public function Person($fname, $lname) {
            echo "Initialising the object...<br/>"; 
            $this->fname = $fname;
            $this->lname = $lname;
        }
        
        // public method to show name
        public function showName() {
            echo "My name is: " . $this->fname . " " . $this->lname . "<br/>"; 
        }
    }
    
    // creating class object
    $john = new Person("John", "Wick");
    $john->showName();
    
?>