InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Defining Object Destructors in PHP
PHP

Defining Object Destructors in PHP

InfinityCoder November 24, 2016

You want to define a method that is called when an object is destroyed. For example, you want to automatically save information from a database into an object when it’s deleted.

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

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

To make PHP call a method when an object is eliminated, define a method named__destruct():

1
2
3
4
5
class car {
   function __destruct() {
      // head to car dealer
   }
}

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 supports object destructors. Destructors are like constructors, except that they’re called when the object is deleted.

Even if you don’t delete the object yourself using unset(), PHP still calls the destructor when it determines that the object is no longer used.

This may be when the script ends, but it can be much earlier. You use a destructor to clean up after an object. For instance, the Database destructor would disconnect from the database and free up the connection.

Unlike constructors, you cannot pass information to a destructor, because you’re never sure when it’s going
to be run.
Therefore, if your destructor needs any instance-specific information, store it as a property:

1
2
3
4
5
6
// Destructor
class Database {
   function __destruct() {
      db_close($this->handle); // close the database connection
   }
}

Destructors are executed before PHP terminates the request and finishes sending data.

Therefore, you can print from them, write to a database, or even ping a remote server.
You cannot, however, assume that PHP will destroy objects in any particular order.
Therefore, you should not reference another object in your destructor, because PHP  may have already destroyed it.

Doing so will not cause a crash, but it will cause your code to behave in an unpredictable (and buggy) manner.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Sorting in a Locale-Aware Order in PHP
You need to sort text in a way that respects …

Sorting in a Locale-Aware Order in PHP

Colorizing Console Output in PHP
You want to display console output in different colors. Use …

Colorizing Console Output in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap