Beamtic's logo
  1. Create user
Home

Share via:

PHP: How to Destroy an Object

How to destroy and object that was instantiated when it is no longer needed by our PHP script.

55 views

Edited: 2020-07-22 23:58

When an object is no longer needed it can often be safely destroyed to free up memory in the running script.

Instantiating an object may look like this:

$some_object = new some_object();

This object might take up memory for the duration that the script is running. PHP will automatically free up the memory when the script finishes running.

To free up memory instantly, we can use unset() on the variable holding the object:

unset($some_object);

This should, however, only be necessary when working with memory-heavy scripts; in addition, we might also benefit from unset in environments with a sufficiently high number of concurrently executing PHP scripts.

Comments

  1. Auto instantiating objects created by factory classes.
  2. Learn what PHP objects are, and how to use them in your own PHP coding adventures.
  3. When does extending classes have a useful purpose? Find out more in this article.
  4. Beginners Tutorial in Object-oriented programming in PHP.
  5. Learn about the difference between arrays and objects, and why objects are more powerful than arrays.

More in: OOP in PHP