Home arrow PHP arrow Page 4 - Understanding Destructors in PHP 5

Testing the Previous User Class - PHP

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



Seeing a destructor method in action: testing the previous user class

If you’re anything like me, you may want to see how the prior “User” class functions after implementing its corresponding destructor. Well, I have to admit that I’m feeling quite curious too. Below I coded another sample script, which demonstrates how the destructor is called when the script finishes its execution, and consequently any object spawned from the previous user class is also destroyed.

Now that I have explained that, please study the following code sample:


// create new instance of 'User' class

$user=new User('John','Doe','john@domain.com');

// display separately user data

echo 'First Name: '.$user->getFirstName().'<br />';

echo 'Last Name: '.$user->getLastName().'<br />';

echo 'Email: '.$user->getEmail().'<br />';

// display all user information

echo 'Complete user information: '.$user->getAll();


catch(Exception $e){

echo $e->getMessage();



Frankly speaking, the above example looks identical to the one that you saw in the prior section. However, this first impression is a bit tricky; if you test this example on your system, you’ll see that it outputs the following on the browser:

First Name: John

Last Name: Doe

Email: john@domain.com

Complete user information: First Name: John Last Name: Doe Email Address: john@domain.com

Destroying user now...

Hey, what’s that last phrase at the end of the output? Yes, you guessed right! Once a user object is created and used in a simple manner, the script finishes running…but before this happens, the pertinent “__destruct()” method is called automatically, in this way displaying the message “Destroying user now…”

At this moment, you’ll have to agree with me that working with destructors in PHP 5 is a very simple process. Naturally, my implementation of the “__destruct()” method in the previous example is primitive, but I believe that this should be clear enough for you to grasp the logic that stands behind using destructors.

My final recommendation is simply that you play with this sample user class and test different implementations for its destructor to see what happens in each case.

Final thoughts

In this initial article of the series I walked you through learning the basic concepts that surround the use of class destructors with PHP 5. As you saw from the sample classes that I built previously, declaring and implementing these special class methods are actually no-brainer processes that can be grasped quickly, even if you’re only starting to work with classes and objects.

In the next part of the series, things will become far more interesting and complex. I'm going to teach you how to work with multiple classes that implement obviously different destructors.

Now that you’ve been warned, you simply can’t miss the next article!

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