PHP Constructor

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.

### 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;

css.php