7.3. Destroying an Object

Problem

You want to eliminate an object.

Solution

Objects are automatically destroyed when a script terminates. To force the destruction of an object, use unset( ):

$car = new car; // buy new car
...
unset($car);    // car wreck

Discussion

It’s not normally necessary to manually clean up objects, but if you have a large loop, unset( ) can help keep memory usage from spiraling out of control.

PHP 4 doesn’t have destructors, however Zend Engine 2 supports them with the _ _destruct( ) method.

See Also

Documentation on unset( ) at http://www.php.net/unset.

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.