PHP - Constructor and Destructor

Every class has two special functions, constructor and destructor. Even if you do not explicitly declare and define them, they exist (they are empty).

Constructor is a function which is called right after you create a new object. This makes constructor suitable for any object initialization you need.

Destructor is a function which is called right after you release an object. Releasing object means that you do not need it or use it anymore. This makes destructor suitable for any final actions you want to perform.

Here we come across first difference between object oriented code in PHP4 and PHP5.

In PHP4 constructor is a function which has the same name as a class. So, if you have a class named MyClass, constructor is a function named MyClass.

In PHP5 constructor is a function called __construct. PHP5 is a backward compatible, so if it can not find a function named __construct in a class declaration, it will search for a PHP4 style constructor (function with a name same as a class) and use it as a constructor.

Destructor is a PHP5 feature. PHP4 does not have destructors at all. According to the PHP Manual, you can use function register_shutdown_function() to simulate destructor behavior, but since we are PHP5 oriented we will not discuss this. Remember that register_shutdown_function is not a destructor, it can just simulate its behavior.

Destructor is a function called __destruct(). As a constructor, this function can not be called directly. It will be called implicitly when you release your object.

When you create inherited class, its constructor will be executed. It may be logical to you that the constructor of a parent class executes too, but this is not a case. If you want to execute parent constructor you have to call it explicitly in a subclass constructor with:

parent::ClassName

or

parent::__construct

This is clearly illustrated in our example. We have just PHP4 style illustrating this. Easy practice for you can be to implement PHP5 style. Just watch constructor names.

In this example, we have several classes defined. We create one object of each class, and then release them. Constructors and destructor are executed.

<?php
### PHP4 Constructor ###
class Person4 {
    var $name;
    var $address;
    var $phone;
    // this is constructor
    function Person4()
    {
        $this->name = "John Doe";
        $this->address = "Unknown";
        $this->phone = "Unknown";
    }
    function printPersonInfo()
    {
        echo $this->name . "\n";
        echo $this->address . "\n";
        echo $this->phone . "\n";
    }
}
### PHP5 Constructor ###
class Person5 {
    var $name;
    var $address;
    var $phone;
    // this is constructor
    function __construct()
    {
        $this->name = "John Doe";
        $this->address = "Unknown";
        $this->phone = "Unknown";
    }
    // this is destructor
    function __destruct() {
        echo "Person5 Object Released\n";
    }
    function printPersonInfo()
    {
        echo $this->name . "\n";
        echo $this->address . "\n";
        echo $this->phone . "\n";
    }
}
class Ext1Person4 extends Person4 {
    // this is a PHP4 constructor
    function ExtPerson4()
    {
        $this->name = "John Doe";
    }
}
class Ext2Person4 extends Person4 {
    // this is a PHP4 constructor
    function ExtPerson4()
    {
        // we explicitly call parent constructor
        parent::Person();
    }
}
$Person4 = new Person4;
$Person4->printPersonInfo();
$Person4 = NULL;
$Person5 = new Person5;
$Person5->printPersonInfo();
$Person5 = NULL;
// When we create this object, only one member will be initialized
// because parent constructor is not executed
$Ext1Person4 = new Ext1Person4;
$Ext1Person4->printPersonInfo();
$Ext1Person4 = NULL;
// When we create this object, all members will be initialized
// because we have called parent constructor from subclass constructor
$Ext2Person4 = new Ext2Person4;
$Ext2Person4->printPersonInfo();
$Ext2Person4 = NULL;
?>

Related Marakana Courses

  • PHP and MySQL Bootcamp Training

16 Responses to “PHP - Constructor and Destructor”

  1. bla Says:

    ok man

  2. goodman Says:

    Thanks, this is just what i needed!

  3. rob Says:

    // When we create this object, only one member will be initialized
    // because parent constructor is not executed
    $Ext1Person4 = new Ext1Person4;
    $Ext1Person4->printPersonInfo();
    $Ext1Person4 = NULL;
    // When we create this object, all members will be initialized
    // because we have called parent constructor from subclass constructor
    $Ext2Person4 = new Ext2Person4;
    $Ext2Person4->printPersonInfo();
    $Ext2Person4 = NULL;
    ?>

    I don't quite understand this part. It looks exactly the same, so what seems to make the difference?

  4. narenthiran Says:

    nice

  5. jcWebDdev Says:

    To rob,

    The difference is that $Ext2Person4 called the parent constructor Person(). But then my understanding stops here . . . because when I run the code (after adding a Person class which really does nothing), I don't see anything different between $Ext1Person4 and $Ext2Person4 (output-wise).

    ???

  6. Dennie Malone Says:

    nbgcmp3ap372o6mr

  7. Rick Says:

    Looks to me like there is a typo, ie parent::Person(); should be parent::Person4();. Then the difference would be that when calling $Ext2Person4, the parent constructor would fire, and assign 3 variable to the class. If you call just Ext1Person4 then only 1 variable is assigned to class. I also noticed that the function names in the PHP4 classes should have the exact same name as the class, otherwise they are not constructors, and instead would be accessed using method calls like $Ext2Person4->ExtPerson4();

  8. zichzach Says:

    Thanks I was looking for this.

  9. deltamaster Says:

    Useful information.
    It is just what I need.
    Thank you very much.

  10. gobi Says:

    Thnxxxxxx it is very useful information..It is just what i need.

  11. mahendra Says:

    this is great class and help \.

  12. hits Says:

    good effect

  13. Divendu Says:

    Good Example, no doubt this is just what i needed!

  14. Louie Grossklaus Says:

    Oh my GOD! now there is a exiting way to fix your RROD I just found out today. Check my link posted. If it does not work try rrod-fix-pro dot com.

  15. Harpreet Says:

    Thanks,,, for all this code.. i really need this

  16. renuka Says:

    thanks man,
    its really help me for my practical viva-voce.

Leave a Reply