PHP Constructors Destructors

While working with PHP object model, we need to set properties of that object, before using it as a reference to access class member functions. For such initial settings, object oriented concepts are there, that are commonly used in many object oriented languages.

Constructors and destructors are such object oriented concepts supported by PHP. Constructors are used for initializing object properties before using it, and destructors are used for destroying object reference.

PHP Constructors Destructors Syntax

In PHP, Constructors and destructors are defined for a class by using PHP magic functions __construct, __destruct, respectively. And their syntax are shown below.

class <class-name> {
function __construct(){
//initializing object properties
}
function __destruct(){
//clearing object reference
}
}

Like other languages, PHP constructors can accept more arguments, where as, destructors will have nothing as its argument, and it is simply void. Because, with destructors, we are just destroying current object reference, which doesn’t require any parameters.

Backward Compatibility

In older PHP versions with older object model, the constructors are defined with the name of the classes, as we have familiar with other object oriented languages. For example,

class <class-name> {
function <class-name>(){
//initializing object properties
}
}

Still, PHP supports this form of constructors to provide backward compatibility for supporting PHP projects developed in older versions of PHP.

So, while creating objects for a class, PHP will search for the magic method __construct(). If not found and there is a function defined with the name of the class, then, PHP will treat it as constructor and will call it automatically, for initializing object properties.

Note:

As of PHP version 5.3.3, if a class is a namespaced class, then, the function with the name of that class will not be treated as constructor, rather, it will be considered as a normal function.

Inheriting Constructors

By using another object oriented concept, inheritance, supported by PHP, we are going to inherit constructors from parent class to child class derived from it.

So, a derived class need not have any constructor separately, if the child is not required any special object initialization of its own. In such scenario, PHP allows to inherit parent class constructor to its child also. For example,

class Toys{
public $name;
public $price;
function __construct($name,$price) {
$this->name = $name;
$this->price = $price;
echo "Properties initialized by parent constructor";
}
function __destruct() {
echo "Object reference is cleared";
}
function get_toys_info() {
$toys["name"] = $this->name;
$toys["price"] = $this->price;
return $toys;
}
}
class Mechanical_Toys extends Toys{

}
$objMechanicalToys = new Mechanical_Toys("Battery Car",500);
$toys_info = $objMechanicalToys->get_toys_info();
print "<PRE>";
print_r($toys_info);
print "</PRE>";

In the above program, we have created a super class Toys, having its own constructor, destructor and a function get_toys_info to return toys details. And then, we have derived a child class Mechanical_Toys from Toys class. We can see with the above code that this child class have no constructor, destructor and no other members.

php_constructors_destructors

After completing class definitions, let us create an object for the child class. Even though, it doesn’t have any members, it can inherit the members of parent class, if we create object with appropriate number of parameters, as its constructor has.

Since we are creating child class object with two parameters, PHP will search for the two argument constructor to be called, automatically. And, we have such constructor with parent class, which will be inherited, and so, child object properties will be initialized. For that PHP $this variable is used for referring those properties in object perspective.

Note:

When the child class has its own constructor and destructor, then, they will be invoked with higher preference, while creating child class objects. Let us see about it in detail with upcoming paragraph.

Overriding PHP Constructors Destructors

By overriding parent class constructors and destructors by that of child class, we can handle some special initialization, which is specifically used for child class objects. For that, we should add another set of __construct() and __destruct magic functions to the child class, as its own. So, the code will be as follows.

class Mechanical_Toys extends Toys{
public $category;
function __construct($name,$price) {
$this->name = $name;
$this->price = $price;
$this->category = "Mechanical Toys";
echo "Properties initialized by child constructor";
}
function __destruct() {
echo "Object reference is cleared by child constructor";
}
}

After that, this newly added child class constructors and destructors will be invoked on child instance creation and exit, respectively. In this child class constructor, one more object property, that is, the toys category is initialized.

Note:

  • Instead of assigning name and price properties once again for child class, we can invoke parent constructor, like, parent::__construct. So, the code will be,

    function __construct($name,$price) {
    parent::__construct($name,$price);
    $this->category = "Mechanical Toys";
    echo "Properties initialized by child constructor";
    }
    

Download PHP Constructor Destructor Source Code

This PHP code tutorial was published on September 4, 2013.

↑ Back to Top