Welcome to the final chapter of the series “Understanding Destructors in PHP 5.” In consonance with this article’s title, this series walks you through the basics of utilizing class destructors in object-based applications and also provides you with a decent number of hands-on examples, so you can start incorporating these useful methods into your PHP 5 classes with minor efforts.
If you’re a PHP developer who has spent a considerable amount of time using the set of brand new features incorporated into the object model of PHP 5, then there’s the possibility that you’ve already worked with class destructors. However, you may want to refresh their foundations or simply fill some gaps regarding their adequate implementation, so don’t hesitate to start reading this article series now!
Now that you hopefully have a clear idea on the purpose of this article series, it’s an appropriate moment to make a brief recapitulation about the items that were discussed in great detail during the previous tutorial of the series. As you’ll surely recall, in that particular installment I demonstrated how to retrieve useful information about a few user handling objects, such as their respective properties and methods, prior to being removed by the PHP engine.
This process was performed successfully by way of a single destructor method whose implementation consisted first of iterating over the properties and methods of a particular object, and then displaying this data on the browser. By using this simple, yet effective, approach it was possible to build a mechanism that printed structural information on a specific object onto the screen right before being destroyed by the PHP interpreter.
All right, at this point I should assume that implementing destructors in a useful way is now a familiar concept to you, which may lead you to erroneously think that the topic doesn’t bear further discussion. Well, not so fast, since there’s an important point that still remains uncovered and here it comes:
As you learned so far, destructors were called automatically by the PHP engine before finishing the execution of a determined script. However, did you ever think that these methods could be forced to run manually? Yes, you’re correct! If you purposely delete an object that implements a destructor, logically it will be called without the need to abruptly terminate the execution of a given script. Sounds quite interesting, right?
Therefore, in this final tutorial of the series I’m going to show you how to manually trigger a destructor, and in addition you’ll learn how to emulate the behavior of this magic method when using PHP 4.
Are you ready to tackle this last chapter of this educational journey? Let’s get started!