PHP Constructor and Destructor: How to Create in PHP With Examples

Here we are going to explore what are the PHP Constructor and Destructor in PHP. How to Create in classes and more.

What are PHP Constructors and Destructors?

We can define Constructors as the very basic Building blocks that help objects and how they would work.

When we initiate objects in PHP, Constructors are called automatically.

And as for the Destructors they are there for destroying objects when execution is done.

Similar to Constructors, Destructors also execute automatically, the difference here is Constructors execute when we initialize the object and Destructors execute in the end of execution.

The __construct Function in PHP

Constructor in PHP is there for initialization for properties of the object during creation for the object.

If we create a constructor function inside a particular class, PHP will run that __construct() function automatically when we create object of that class.

In PHP there are two types of constructors-

  • Default Constructor: This kind of constructor has no parameter.
  • Parameterized Constructor: If we add parameters inside a constructor, it would be called a parameterized constructor.

Here is the syntax for creating a constructor function in PHP-

function __construct($n){
    #code to run
}

We add this function inside the class.

Here $n inside parenthesis refers to the properties, we can add multiple properties to it separated by commas.

and when we create objects we can put the value inside parenthesis to directly assign them.

Here is a complete example-

class Students{
    public $name;
    public $rollNumber;
    
    // Creating a Constructor
    function __construct($name){
        $this->name = $name
    }
    
    // Creating getter
    function get_name() {
    return $this->name;
  }
  
}

$ram = new Student("Ram Verma");

echo $ram->get_name();

Here are a few things about class constructors-

  • Constructors run automatically when the object is created.
  • We create this function inside the class by writing function __construct
  • In between parentheses of __construct function, we can add properties as parameters.
  • Constructors with no parameters are called default constructors.
  • A constructor with a parameter is called the parameterized constructor.

The __destruct function in PHP

Destructors are the opposite of Constructors.

A Destructor is executed automatically when either object is destroyed, the script is stopped, or the user exits the script.

Similar to the Construct function Destruct also starts with two underscores.

Here is the basic syntax for the Destruct function-

function __destruct(){
    // Code Goes here.
}

Here is how we can create destruct function inside a class:

class Student{
    public $name
        
    function __construct($name) {
        $this->name = $name;
    }
    
    function destruct(){
        echo "Name of Student is {$this->name}.";
    }
}

$ram = new Student("Ram Verma");

Since the destruct function run at the end of the script we will see this text-

Name of Student is Ram Verma.

Here are the things that we learned about Destructor:

  • Destruct Function will run when the object is destroyed, or end of script execution.
  • This function will run automatically in the end.
  • We would use Destruct to do things like close connections or run code blocks that we want to run in the end.