Constructors and Destructors

Jun 29, 2009 Author: City Hall

Constructors...

PHP 5 introduces the concept of the unified constructor and, along with it, a new destructor for objects. The constructor and destructor are special class methods that are called, as their names suggest, on object creation and destruction, respectively. Constructors are useful for initializing an object’s properties, or for performing startup procedures, such as, for example, connecting to a database, or opening a remote file. The concept of the constructor is, of course, not new to PHP 5. In PHP 4, it was possible to define a class method whose name was the same as the class itself; PHP would then consider this method to be the class’ constructor and call it whenever a new instance of the class was created. This approach had several drawbacks—for example, if you decided to rename your class, you would also have to rename your constructor. To avoid these problems, PHP 5 now uses the magic __construct() method as the constructor for any class regardless of the class’ name. This greatly simplify things, and provides you with a standard mechanismto recognize and call constructors in a consistent manner:

class foo {
function __construct()
{
echo __METHOD__;
}
function foo()
{
// PHP 4 style constructor
}
}
new foo();

This example will display foo::__construct (the __METHOD__ constant is replaced at compilation time with the name of the current class method). Note that, if the __construct() method is not found, PHP will look for the old PHP 4-style constructor (foo) and call that instead.

Destructors...

In addition to the __construct() method, we also have a __destruct() method. This works like a mirror image of __construct(): it is called right before an object is destroyed, and is useful for performing cleanup procedures—such as disconnecting from a remote resource, or deleting temporary files:

class foo {
function __construct()
{
echo __METHOD__ . PHP_EOL;
}
function __destruct()
{
echo __METHOD__;
}
}
new foo();

This code will display:

foo::__construct
foo::__destruct

Destruction occurs when all references to an object are gone, and thismay not necessarily take place when you expect it—or even when you want it to. In fact, while you can unset() a variable that references an object, or overwrite it with another value, the object itself may not be destroyed right away because a reference to it is held elsewhere. For example, in the following script the destructor is not called when calling unset(), because $b still references the object:

$a = new foo();
$b = $a;
unset($a);

Even if an object still has one or more active references, the __destruct() method is called at the end of script execution—therefore, you are guaranteed that, at some point, your destructor will be executed. However, there is no way to determine the order in which any two objects in your scripts will be destroyed. This can sometimes cause problems when an object depends on another to perform one or more functions—for example, if one of your classes encapsulates a database connection and another class needs that connection to flush its data to the database, you should not rely on your destructors to perform a transparent flush to the database when the object is deleted: the instance of the first class that provides database connectivity could, in fact, be destroyed before the second, thus making it impossible for the latter to save its data to the database.


views 5345
  1. Add New Comment

gravatar
Bill Getas
February 26,2011 at 20:22:19
Eeek, it seems like _destruct() was somewhat of an afterthought, as its non-deterministicism causes huge problems for Dependency Injections. Hopefully PHP6 will provide a FILO for object destruction and also a __pre_destruct() method which is called prior to object obliteration. One specific case needing known destruction is LOGIC object using DATA object to save something to DB...very ungood if DATA obj gets nixxed before LOGIC!