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
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();
?>
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.
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 DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT