Destroying Objects

True object-oriented programming languages include the ability to destroy an instance of an object using destructors, the opposite of a constructor. Whereas a constructor is immediately called upon creation of an object, a destructor would be automatically called when an object is destroyed. PHP does not have this capability built-in, but you can replicate it to a degree.

Should you want to free up the resources used by an object that's no longer necessary, start by deleting the value of the instance using the unset() function.

$object = new ClassName; 
unset ($object);

As a simple example of destroying an object, I'll modify the instance5.php script.

To destroy an object:

1.
Open instance5.php (Script 2.14) in your text ...

Get PHP Advanced for the World Wide Web: Visual QuickPro Guide 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.