PHP Class Constructors and Destructors

A constructor is a function defined in a PHP class so it can also be termed as class constructor. Constructor function is automatically called when an object of the class is created. As soon as $object = new MyClass(); statement executes, the constructor function of the class will be executed.

In this articles, we are going to learn about the object oriented concepts of constructors and destructors in php.

PHP Class Constructor

In PHP4, the constructor function has the same name as that of the class it is defined within. But from PHP5, constructor can be created by defining a function __construct().

Syntax of constructor in PHP

function __construct()
{
   // initialize instance variables with values
}

Syntax of destructor in PHP

function __destruct() 
{
   // destroying the object or clean up resources here
}
Note:Note that double underscore (__) is used in function name for both constructor and destructor.

Examples of Constructor and Destructor

This code defines a constructor in PHP4 style.
<?php

class Student
{
   var $name;
   var $age;
   var $email;

   // this is constructor
   function Student()
   {
      $this->name = "John";
      $this->age = 46;
      $this->email = "xyz@gmail.com";
   }

   function displayInfo()
   {
      echo "Name: $this->name \n";
      echo "Age; $this->age \n";
      echo "Email: $this->email \n";
   }
}

$S1 = new Student();
$S1->displayInfo();
$S1 = NULL;
?>

There is no static property in objects in PHP4, and there is no destructor in PHP4 objects. Whenever an object is copied, it is a shallow copy of that object.

This code defines a constructor in PHP 5 style.
<?php

class Employee
{
   var $eno;
   var $name;
   var $age;
   var $sal;

   // this is constructor
   function __construct()
   {
      $this->eno = "John";
      $this->name = "John";
      $this->age = 46;
      $this->sal = 100000;
   }

   // this is a destructor
   function __destruct()
   {
       echo "Employee object released.\n";
   }

   function displayInfo()
   {
      echo "Employee No: $this->eno \n";
      echo "Name: $this->name \n";
      echo "Age; $this->age \n";
      echo "Salary: $this->sal \n";
   }
}

$emp = new Employee();
$emp->displayInfo();
$emp = NULL;
?>

If you define a constructor with name of class as name of constructor function it is still valid because PHP5 is backwards compatible.

PHP Multiple Constructors

Types of constructors in PHP

  • Default Constructor: It has no parameters, but the values to the default constructor can be passed dynamically.
  • Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.
  • Copy Constructor: It accepts the address of the other objects as a parameter.

PHP Parameterized Constructor

We have seen default constructor in above examples. For parameterized constructor, the constructor function takes in some arguments, thats the only difference between default and parameterized constructor. The object creation will also change accordingly, for example

class Employee
{
 //data members

 //parameterized constructor
 // it can take any number of arguments
 function __construct($arg1, $arg2,$argN)
 {
   // initialize the data members here
 }
}

// object creation
// pass the some values as arguments
$object = new Employee($val1, $val2, $valN);

Learn more about object cloning.

Go take a look at this article PHP OOP Constructors. You can run the code with their online IDE and understand better.