Home arrow PHP arrow Page 4 - Executing Destructors Manually in PHP 5

Emulating destructors in PHP 4 - PHP

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.

  1. Executing Destructors Manually in PHP 5
  2. Triggering a destructor manually
  3. Calling multiple destructors manually
  4. Emulating destructors in PHP 4
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
February 13, 2008

print this article



While PHP 4 indeed doesn’t directly support the implementation of destructors, as does PHP 5, the behavior of these methods can be emulated by using the PHP “register_shutdown_function()” function, which, as its name suggests, permits it to specify what user-defined function will be called when a script terminates its execution.

Of course, this function can be used with both procedural and object-oriented approaches. In this case I’m going to teach you how to use it within the same “User” class that you saw in previous sections, allowing the definition of a concrete method that will be invoked when a particular user handling object is destroyed by the PHP interpreter.

Naturally, this behavior closely resembles the one exposed by a destructor in PHP 5. So in taking advantage of this, below I listed the signature of the prior “User” class in order to function with PHP 4. Here’s how the pertinent class now looks:

// define 'User' class

class User{

var $firstName;

var $lastName;

var $email;

function User($firstName,$lastName,$email){


trigger_error('Invalid First Name parameter!',E_USER_ERROR);



trigger_error('Invalid Last Name parameter!',E_USER_ERROR);



 trigger_error('Invalid Email parameter!',E_USER_ERROR);







// get user's first name

function getFirstName(){

return $this->firstName;


// get user's last name

function getLastName(){

return $this->lastName;


// get user's email

function getEmail(){

return $this->email;


// get all user data

function getAll(){

return 'First Name: '.$this->firstName.' Last Name: '.$this->lastName.' Email Address: '.$this->email;


// define class method for being called when the script finishes its execution

function customShutdown(){

// display object properties

echo '<h2>Properties of current object</h2>';

foreach(get_object_vars($this) as $prop=>$val) {

 echo '<p>'.$prop.'='.$val.'</p>';


// display object methods

echo '<h2>Methods of current object</h2>';


foreach($methods as $method) {

echo '<p> Method Name: '.$method.'()</p>';




As you can see, at first sight the above user handling class looks very similar to its PHP 5 incarnation; however, you should pay attention to the definition of its constructor, since here is where all the action takes place. The following line within this class method:


indicates that a “customShutdown()” method will be called when an object of the “User” class is deleted, in this manner emulating the behavior of a real destructor. Quite simple, right?

Assuming that you already grasped how to implement the PHP “register_shutdown_function()” as part of a class API, please study the following example, which demonstrates how the “customShutdown()” method is neatly called when a sample user object is destroyed by the PHP engine. Here it is:

// create user object

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

// display separately user data

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

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

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

// display all user information

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

/* displays the following

First Name: John

Last Name: Doe

Email: john@domain.com

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

Properties of current object




Methods of current object

Method Name: User()

Method Name: getFirstName()

Method Name: getLastName()

Method Name: getEmail()

Method Name: getAll()

Method Name: customShutdown()


In this case, you can see that the previous “User” class now has a method that closely resembles the behavior of the native destructor in PHP 5, since when the above script finishes running (and the pertinent user object is consequently deleted), the “customShutdown()” method is properly invoked.

Here you have it. From this point onward, you may want try defining some classes with PHP 4 and implementing a few pseudo destructor methods within them. It’s going to be an instructive experience, believe me!

Final thoughts

It’s hard to believe, but we’ve come to the end of this series. Overall, the experience has been instructive and also fun. As you saw in the different tutorials, destructors aren’t going to change your life as a PHP developer forever, but they can be potentially useful in certain cases.

Tracking the status of a bunch of objects is probably one of its most popular uses, but this doesn’t necessarily mean that they can only be applied to perform this task. So fire up your inspiration and start using destructors in a creative way!

See you in the next PHP development tutorial!

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