RSS

Tag Archives: Destructor

Destructors in PHP

Last time i had written blog on `Constructor`. But left the `Destructor` part. So today in this blog we will figure out the destructor. The destructor method is called as soon as all the references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. This makes destructor suitable for any final action we want to perform after the release of an object. Like to free up resources once we have done with them.

In PHP 5 and successive versions we describe Destructor as :

function _destruct() {

}

Like constructors parent destructor will not be called implicitly by the engine. In order to run parent destructor, we have to explicitly call as parent::_destruct(); in the destructor body.

Destructors called during the script shutdown have HTTP headers already sent. Attempting to throw an exception from a destructor which called in the time of script termination causes fatal error.

 
Leave a comment

Posted by on June 22, 2011 in OOPs, PHP

 

Tags: , , , , , ,

Constructors and Destructors in PHP

PHP 5.3.3 allows us to declare “Constructors” and “Destructors” in a new way. Before this, method with the same name with-in a class treated as “Constructor”. But now we declare constructor as :

__construct() {

// Generally used for initializing the variables

}

And we declare destructor as :

__destruct() {

//Generally used to release memory or make free the resources

}

“Constructor” called on each newly created object and “Destructor” called when the object is explicitly destroyed or in any order in shutdown sequence.

 
Leave a comment

Posted by on November 19, 2010 in PHP

 

Tags: , , , , ,