PHP
  Home arrow PHP arrow Page 4 - Implementing Destructors with Multiple Objects in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Implementing Destructors with Multiple Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek