PHP
  Home arrow PHP arrow Page 4 - Implementing Destructors with Multiple...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Implementing Destructors with Multiple Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-01-23

    Table of Contents:
  • Implementing Destructors with Multiple Objects in PHP 5
  • Handling user-related data with a simple PHP 5 class
  • Working with multiple objects by redefining the destructor of the previous “User” class
  • Finding out the order in which several destructors are called by the PHP interpreter

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Implementing Destructors with Multiple Objects in PHP 5 - Finding out the order in which several destructors are called by the PHP interpreter


    (Page 4 of 4 )

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In the first episode of the series, I introduced you to using destructors in PHP 5,...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway