Hacking with PHP has been updated for PHP 7 - click here! >>

Deleting objects

So far our objects have been automatically destroyed at the end of the script they were created in, thanks to PHP's automatic garbage collection. However, you will almost certainly want to arbitrarily delete objects at some point in time, and this is accomplished using unset() in the same way as you would delete an ordinary variable.

Calling unset() on an object will call its destructor before deleting the object, as you would expect.


If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Copying objects >>

Previous chapter: Destructors

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.