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

Triggering a destructor manually - 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



As I said in the beginning, it’s perfectly possible to call the destructor of a determined object without having to end the execution of an application or script. Since in all the cases a destructor will be invoked when its pertinent object no longer exists, this process can be triggered manually simply by removing the  object via the “unset()” PHP native function.

As you’ll possibly know, this function comes in handy for removing unwanted variables of all types, and these also include objects. The concept is theoretically interesting, therefore I’m going to demonstrate how to manually call a destructor by using the same “User” class that you saw in previous articles of the series.

Its respective definition was the following:

// define 'User' class

class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){


throw new Exception('Invalid First Name parameter!');



throw new Exception('Invalid Last Name parameter!');



throw new Exception('Invalid Email parameter!');






// get user's first name

public function getFirstName(){

return $this->firstName;


// get user's last name

public function getLastName(){

return $this->lastName;


// get user's email

public function getEmail(){

return $this->email;


// get all user data

public function getAll(){

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


// implement a __destruct()' method

public function __destruct(){

// display object properties

echo '<h2>Properties of object being destroyed</h2>';

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

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


// display object methods

echo '<h2>Methods of object being destroyed</h2>';


foreach($methods as $method) {

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




Now that you hopefully recalled how the above class does its business, let me show you a concrete example where its respective constructor is called manually using the popular “unset()” PHP function.

Having said that, here’s how this brand new example looks:


// create user

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

// display separately user data

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

// delete object, so this triggers its '__destruct()' method


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

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

// display all user information

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


/* displays the following:


First Name: John


Properties of object being destroyed






Methods of object being destroyed


Method Name: __construct()

Method Name: getFirstName()

Method Name: getLastName()

Method Name: getEmail()

Method Name: getAll()

Method Name: __destruct()

Fatal error: Call to a member function getLastName() on a non-object in path/to/example




catch(Exception $e){

echo $e->getMessage();



After studying the previous example, you’ll have to admit that things suddenly become exciting! As you can see, once a generic user handing object is created by the above script, the object in question is deliberately deleted by way of the aforementioned “unset()” PHP function.

This trick results in the immediate execution of its destructor, certainly a process shown clearly by the output generated by the prior script. Now, at this point, do  you see how easy it is to make a destructor run without waiting for a script to finish? I guess you do!

However, the example that you just learned uses only one object along with its corresponding destructor. But what about using multiple objects and destructors that are called manually? Indeed, it sounds like something promising and useful. Thus in the next section, I’m going to show you precisely how to fire up different destructors by using the “unset()” PHP function utilized previously.

To learn the details of how this process will be achieved, please click on the link below and keep reading.

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