PHP
  Home arrow PHP arrow Page 2 - Keeping Track of Objects when Using Destructors 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

Keeping Track of Objects when Using Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek