Home arrow PHP arrow Page 4 - Implementing Destructors with Multiple Objects in PHP 5

Finding out the order in which several destructors are called by the PHP interpreter - PHP

Welcome to the second installment of the series “Understanding Destructors in PHP 5.” Whether you’re a PHP programmer who’s making their first steps in the area of professional PHP development, or an experienced developer with a solid background in object-based PHP programming, this article series can provide you with an approachable guide for using destructors in PHP 5-controlled development environments.

TABLE OF CONTENTS:
  1. Implementing Destructors with Multiple Objects in PHP 5
  2. Handling user-related data with a simple PHP 5 class
  3. Working with multiple objects by redefining the destructor of the previous “User” class
  4. Finding out the order in which several destructors are called by the PHP interpreter
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
January 23, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In consonance with the concepts I deployed in the section you just read, I'm going to code a basic script that will create multiple instances of the “User” class that you saw previously. Then, it will echo on the browser the respective first and last names of these user objects just created. And finally, it will finish its normal execution.

However, since each of these objects implements a destructor, they will be called logically by the PHP parser, which will also help you know the order in which the respective destructors are invoked before the objects in question are destroyed. Sounds like a simple yet educational experiment, right?

Having said that, here’s the script that works with multiple destructors, so study its source code:

try{

// create first user

$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();

 

// create second user

$user2=new User('Mary','Smith','mary@domain.com');

// display separately user data

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

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

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

// display all user information

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

 

// create third user

$user3=new User('Susan','Norton','susan@domain.com');

// display separately user data

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

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

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

// display all user information

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

 

 

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

As you can see in the above example, three different objects are created using the corresponding “User” class, and then their properties are outputted to the browser. Finally, the program stops its execution. So far, nothing unexpected happens. But if you look into the output generated by the previous script, guess what? Yes, you’re correct! Their pertinent destructors are called up in the same order used to spawn the originating objects.

This process is clearly demonstrated by the following listing:

First Name: John

Last Name: Doe

Email: john@domain.com

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


First Name: Mary

Last Name: Smith

Email: mary@domain.com

Complete user information: First Name: Mary Last Name: Smith Email Address: mary@domain.com


First Name: Susan

Last Name: Norton

Email: susan@domain.com

Complete user information: First Name: Susan Last Name: Norton Email Address: susan@domain.com

 

Warning! Destroying User: John Doe

Warning! Destroying User: Mary Smith

Warning! Destroying User: Susan Norton


At this point, I've provided you with an instructive example on how to work with multiple objects that concretely implement their corresponding destructors, a process that came in handy for seeing the order in which they’re called by the PHP interpreter.

My final recommendation concerning the use of destructors is that you develop your own test examples based on the concepts that you learn in this article series. You’re going to have fun, trust me!

Final thoughts

In this second installment of the series, I went a bit deeper into the implementation of destructors with PHP 5 and showed you how to keep track of the order in which these class methods are invoked by the PHP engine when working with multiple objects.

In the upcoming article, I’m going to teach you how to define destructors in such a way that they can display useful information about a particular object prior to being destroyed by the PHP interpreter.

Want to learn how this will be done? Don’t miss the next part!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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