Despite the scary-sounding name, destructors aren't evil. Indeed, their use is often necessary to help keep your applications running properly. This article, the first part of a five-part series, introduces you to destructors and their use with PHP 5 programs.
Generally, people tend to associate the term “destructor” with something that will be used with bad intentions. And to be frank, this attitude has some common sense behind it. After all, if there is such a thing with that scary name, well, it must be evil, and capable of destroying other stuff also.
Fortunately, in the terrain of software development, things aren’t really so drastic really (excepting when your boss isn’t in the mood for jokes). The word "destructor" relates to certain methods or functions of a determined programming language that are responsible for destroying data that, in the context of that particular language, shouldn’t exist any longer.
Speaking more specifically, PHP has a certain number of native routines that are clear examples of “destructive” behaviors that can be healthy during the execution of a specific application. To cite a paradigmatic example, consider its garbage recollection mechanism. It is charged with removing expired session data, useless database links, and so forth. Undoubtedly, under determined conditions, functions or methods that destroy certain data are not only useful, but completely mandatory.
However, aside from the intrinsic destruction mechanisms provided by PHP, there’s another one that was introduced within the improved object model of PHP 5, which actually will be subject of this article series. Obviously, if you read this tutorial’s title, then you’ll possibly know what I’m talking about: the so-called destructor methods.
In case you didn’t know, PHP 5 provides programmers with the capacity for defining inside a given class, a special method called “__destruct()”, which as its name suggests, will be automatically invoked when an object instantiated from that class no longer exists in the context of a certain application.
In the same way that a constructor is executed automatically when a new object is created in PHP, their “__destruct()” counterpart will be called up when the object in question is removed from the web server’s memory. Continuing with the similarities between constructors and destructors, destructors also have a non-default implementation, which gives programmers the ability to define them according to their specific needs.
Now that I provided you with a brief introduction to what destructors are in PHP 5, in this series of articles I’m going to show you the basics of how to work with them. The corresponding theory will be complemented with illustrative hands-on examples that you’ll be able to test and study easily, so you can start using them with your own PHP 5-driven applications.
At this point, it’s time to get rid of the preliminaries and begin learning how to use destructors with PHP 5, so let’s move on and start this instructive journey right now!