PHP
  Home arrow PHP arrow Page 2 - 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
     
     
     
    ADVERTISEMENT

    Virtual Tradeshows by Ziff Davis Enterprise – A Unique Opportunity to Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Keeping Track of Objects when Using Destructors in PHP 5 - When the order really matters: working with multiple destructors
    (Page 2 of 4 )

    There's the possibility that by the time you read the introduction of this article, using multiple destructors in the same PHP 5 application will be a well-known process for you, since this particular situation was analyzed in detail in the previous article of the series.

    However, if you didn't have the chance to go through that specific tutorial, you might want to take a close look at the following PHP 5 script, which precisely recreates a hypothetical scenario in which several user handling objects are first spawned from a sample "User" class, and then destroyed when the script in question finishes its execution.

    Although it's obvious, the most relevant thing to highlight here is that all these objects implement a destructor concretely, which is then called in the proper sequence by the PHP 5 interpreter prior to being removed from the web server's memory.

    That being said, here's the signature of the aforementioned "User" class, so examine it closely:


    // define 'User' class (it provides a slightly more complete implementation of its destructor)

    class User{

    private $firstName;

    private $lastName;

    private $email;

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

    if(!$firstName||strlen($firstName)>32){

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

    }

    if(!$lastName||strlen($lastName)>32){

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

    }

    if(!$email||!preg_match("/^.+@.+..+$/",$email)){

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

    }

    $this->firstName=$firstName;

    $this->lastName=$lastName;

    $this->email=$email;

    }

    // 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(){

    echo '<h2>Warning! Destroying User: '.$this->firstName.' '.$this-
    >lastName.'<h2>';

    }

    }


    Undeniably, the definition of the prior "User" class is very straightforward, so I assume that you shouldn't have much trouble understanding how it works. You should pay special attention, though, to the implementation of its destructor method, which as you can see, is capable of outputting the values of some class properties, such as the ones named ($firstName and $lastName), when automatically called by the PHP parser.

    With such a useful destructor method at your disposal, it's perfectly possible to build a sample script that first creates different user-related objects, and then displays the data of the current object being destroyed. In this way it constructs an effective mechanism that demonstrates the order followed by the PHP engine to call up the respective destructors.

    With this idea in mind, below I coded this simple example, which works with multiple objects and destructors. Here it is:


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

    }


    // output generated by the previous script


    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


    That was pretty easy to grasp! In this case, all the destructors corresponding to the previous user handling objects will be invoked in the same order utilized for creating them. Certainly this is a procedure demonstrated by the above script's output.

    So far, everything seems to be fine, because you've hopefully learned how to work with several objects that concretely implement their respective destructors. However, now that you've digested all this useful material, it's more than probable that you might have asked yourself the following question: is it possible to implement a destructor that can keep track of the properties and methods of a determined object prior to its merciless destruction? (That was pretty dramatic...) Well, the answer is an emphatic yes! And bearing this in mind, in the next section I'm going to show you how to modify the destructor of the same "User" class defined previously so it can provide detailed information about a particular object right before that object is destroyed by the PHP engine.

    To see how this will be done, please click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · 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 6 hosted by Hostway