Home arrow PHP arrow The Destruct Magic Function in PHP 5

The Destruct Magic Function in PHP 5

Welcome to the sixth part of a seven-part series that shows you how to use the magic functions that come with PHP 5. In this article, I cover the “__destruct()” method, also known as a destructor. They can be really useful for performing all sorts of clean-up tasks, or for creating objects that are capable of maintaining their state across several HTTP requests.

  1. The Destruct Magic Function in PHP 5
  2. Review: the clone magic function
  3. Working with destructors in PHP 5
  4. Creating a persistable object with the destruct method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
June 23, 2009

print this article



To be frank, learning how to implement and use the magic functions that come included with PHP 5 isn’t as hard as it may seem at first glance. The entire process requires knowing which event triggers a particular function, and then giving that function a concrete definition in accordance with the specific needs of the application being developed.

So, if you’re a PHP programmer who is searching for a comprehensive guide that introduces you gently to using the most important magic methods provided by PHP 5, then this group of articles may be the material that you need to start reading right away! 

If you already went through all of the articles that precede this one, then it’s probable that by now you have a solid background in working with the most relevant PHP 5 magic functions. This includes, among others, the handy trio comprised of the “__set()”, “__get()” and “__call()” methods; the “__sleep()” and “__wakeup()” functions; and the “__clone()” method, which was explored in depth in the last tutorial of this series. 

However, it’s fair to say that the aforementioned functions aren’t the only ones that you can use within your classes. There are a few others that can be really useful for performing relatively complex tasks with a minimal amount of code. That’s exactly the case with the “__destruct()” method, which will be called automatically by the PHP engine before destroying an instance of a particular class at the end of a script. 

Indeed, implementing destructors is itself a huge subject that is out of the scope of this series for now. Yet, during this tutorial I’m going to discuss how to add a destructor to the sample “User” class that you saw in previous articles, so you can learn how to use it for constructing simple persistable objects. 

Now, it’s time to get rid of the dull theory and start discovering how to use the “__destruct()” PHP 5 magic method. Let’s jump in!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: