Tag Archives: php examples

PHP: Don’t Call the Destructor Explicitly

“PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++”[1] says the documentation for destructors, but let’s see the following class.

class A
{
	public function __construct()
	{
		echo 'building an object';
	}
 
	public function __destruct()
	{
		echo 'destroying the object';
	}
}
 
$obj = new A();

Well, as you can not call the constructor explicitly:

$obj->__construct();

So we should not call the destructor explicitly:

$obj->__destruct();

The problem is that I’ve seen this many times, but it’s a pity that this won’t destroy the object and it is still a valid PHP code.
Continue reading PHP: Don’t Call the Destructor Explicitly