Broad Network


Constructors and Destructors in PHP Classes

Object Oriented Programming in PHP – Part 2

Forward: In this part of the series, we look at PHP constructor and destructor.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 2 of my series, Object Oriented Programming in PHP. In the previous part of the series, we saw how to include default property values in a class. Those default values are like initial values for the class and its instantiated objects. I said that having default values was not common practice. I said that it is good to give initial values for the properties when instantiating an object. In that way, only the object will have the initial values; the class will not have. In this part of the series, we look at PHP constructor and destructor.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The Constructor Function
PHP has a predefined function called, __construct(). This function is used to instantiate an object and at the same time initializing the object (not the class).

The __construct() Function
This is the constructor function. The syntax of this predefined function is:

    void __construct ([ mixed $args [, $... ]] )

The function returns nothing. The arguments of the function are used to initialize the properties of the object. You are the one to type the content of the block of the function. What go into the block are normally initialization statements of properties of the object. Read and try the following program that illustrates this:

<?php

    class Cla
        {
            public $var1;
            public $var2;

         function __construct($v1, $v2)
                {
                    $this->var1 = $v1;
                    $this->var2 = $v2;
                }
    
            public function display()
                {
                    echo $this->var1 . " and " . $this->var2;
                }
        }

    $obj = new Cla("man", "woman");

    $obj->display();

?>

What is actually predefined in this function is just the name of the function. You the programmer determine the content of the argument list and the statements in the function block. In this way, as you instantiate an object, you initialize the object and not the class. This is common practice. The constructor function is called during instantiation. The arguments sent in the parentheses of the instantiation statement become the arguments of the constructor function.

Having a custom constructor function in the code of your class definition as done above, is optional. If you do not provide a constructor function, PHP provides a default one for you, unknown to you. In the case of a default constructor function, no initialization is done; there are no arguments.

Note: When instantiating an object you type the values for initialization as arguments in the parentheses of the class name (after new); you have to provide a constructor function (custom) in this case.

The Destructor Function
When an object is no longer needed in a program, PHP calls a destructor function that destroys the object. PHP calls a default destructor function unknown to you to do the job. You can however add certain features to the destructor function as in the following section.

The __destruct() Function
This is the destructor function. Its syntax is:

        void __destruct ( void )

This function returns nothing and takes nothing as argument. You can add features to the destructor function as in the following program:

<?php

    class Cla
        {
            public $var1;
            public $var2;

         function __construct($v1, $v2)
                {
                    $this->var1 = $v1;
                    $this->var2 = $v2;
                }

         function __destruct()
                {
                    echo "Destroying the object.";
                }
    
            public function display()
                {
                    echo $this->var1 . " and " . $this->var2 . "<br />";
                }
        }

    $obj = new Cla("man", "woman");

    $obj->display();

?>

Having a custom destructor function as above, is optional.

The custom constructor or destructor function is typed into the class definition.

Let us stop here for this part of the series and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message