PHP
  Home arrow PHP arrow Page 4 - Keeping Track of Objects when Using De...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

Keeping Track of Objects when Using Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-01-30

    Table of Contents:
  • Keeping Track of Objects when Using Destructors in PHP 5
  • When the order really matters: working with multiple destructors
  • Retrieving useful information about a particular object before it's removed
  • Seeing the improved User class in action

  • 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
     
     
    CIO Insight
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Keeping Track of Objects when Using Destructors in PHP 5 - Seeing the improved User class in action
    (Page 4 of 4 )

    In the course of the previous section, I showed you how take advantage of the destructor that corresponds to the sample "User" class in order to display a few relevant values of a particular object, prior to its being removed from the web server's memory.

    Based upon the improved implementation of this method, in the next few lines I coded an illustrative example, which, as you'd expect, first spawns a new user handling object from the previous "User" class, and then outputs to the browser the names of the methods and properties assigned to it, right before being deleted by the PHP interpreter. In this way it constructs a primitive, yet effective, mechanism that comes in handy for tracking objects that are just about to be destroyed.

    Now study the following example, which looks like this:


    try{

    // create user object

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

    // display separately user data

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

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

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

    // display all user information

    echo 'Complete user information: '.$user->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 object being destroyed

    firstName=John

    lastName=Doe

    email=john@domain.com

     

    Methods of object being destroyed

    Method Name: __construct()

    Method Name: getFirstName()

    Method Name: getLastName()

    Method Name: getEmail()

    Method Name: getAll()

    Method Name: __destruct()

     

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    See how simple it is to track objects with a simple destructor before they are deleted? I bet you do! In this specific case, this destructor is especially useful, because of its capacity to display on the browser the methods and properties of the object that will be removed.

    However, as you'll realize, the implementation of this method can be improved based on the example that you learned earlier. The same approach that I used here can be extended, for instance, to save the status of the object to a text file or a database table. The possibilities are really numerous. So what are you waiting for? Fire up your creativity and start taking advantage of destructors now!

    Final thoughts

    In this third installment of the series I went a bit deeper into the implementation of destructors with PHP 5 and showed you how to use this magic method to build a simple application that displays the properties and methods of a particular object prior to its corresponding deletion by the PHP interpreter.

    In the next article of the series, I'll teach you how to use the same approach shown in this article, but this time to keep track of multiple objects before being destroyed.

    Curious about 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 course of this third episode of the series, you’ll learn how to retrieve...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




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