Home arrow PHP arrow Understanding Destructors in PHP 5

Understanding Destructors in PHP 5

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.

  1. Understanding Destructors in PHP 5
  2. Building a Sample
  3. Introducing Class Destructors
  4. Testing the Previous User
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
January 16, 2008

print this article



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!

>>> 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: