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.
### 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
October 17th, 2007 at 10:25 pm
ok man
December 5th, 2007 at 4:28 pm
Thanks, this is just what i needed!
July 18th, 2008 at 10:20 am
// 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?
July 29th, 2008 at 2:43 am
nice
October 7th, 2008 at 11:38 am
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).
???
November 12th, 2008 at 5:56 pm
nbgcmp3ap372o6mr
January 21st, 2009 at 12:23 pm
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();
February 22nd, 2009 at 1:14 pm
Thanks I was looking for this.
April 24th, 2009 at 5:52 am
Useful information.
It is just what I need.
Thank you very much.
August 12th, 2009 at 9:20 am
Thnxxxxxx it is very useful information..It is just what i need.
November 29th, 2009 at 8:39 pm
this is great class and help \.
December 12th, 2009 at 7:35 am
good effect
January 4th, 2010 at 3:23 am
Good Example, no doubt this is just what i needed!
April 9th, 2010 at 6:24 am
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.
July 9th, 2010 at 8:51 pm
Thanks,,, for all this code.. i really need this
July 10th, 2010 at 3:41 am
thanks man,
its really help me for my practical viva-voce.
August 11th, 2010 at 11:51 pm
ok.. I must need it