SunQuest
 
       PHP
  Home arrow PHP arrow Page 2 - Retrieving Information on Several Obje...
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 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
SunQuest
 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

Retrieving Information on Several Objects with Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2008-02-06

    Table of Contents:
  • Retrieving Information on Several Objects with Destructors in PHP 5
  • Keeping track of a single object
  • Working with different user handling objects
  • Displaying data related to different objects using one single destructor

  • 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
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Retrieving Information on Several Objects with Destructors in PHP 5 - Keeping track of a single object


    (Page 2 of 4 )

    We'll start demonstrating how to use a destructor to keep track of the methods and properties of multiple objects by recreating the hands-on example developed in the preceding article of the series. In that part I showed you how to perform this task, but using only one instance of the sample “User” class.

    So, with that idea in mind, below I included the signature of this user handling class, along with the definition of a basic script, which outputs to the browser the properties and methods of a generic user object prior to being deleted by the PHP engine.

    The pertinent code sample is as follows:


    // define 'User' class


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

    // display object properties

    echo '<h2>Properties of object being destroyed</h2>';

    foreach(get_object_vars($this) as $prop=>$val) {

     echo '<p>'.$prop.'='.$val.'</p>';

     }

    // display object methods

    echo '<h2>Methods of object being destroyed</h2>';

    $methods=get_class_methods(get_class($this));

    foreach($methods as $method) {

    echo '<p> Method Name: '.$method.'()</p>';

    }

    }

    }


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

    }


    After studying in detail the above example, you’ll have to agree with me that destructors can be pretty useful for logging purposes. As you can see, this particular case shows how to use this magic method to print the values assigned to the pertinent properties of a simple user object, in addition to showing the names of its method.

    This example itself is a no-brainer, but it should give you an approximate idea of how to use a destructor to develop an effective log mechanism that can be triggered before one or more objects no longer exist in the context of a given PHP 5 application.

    Please focus your attention again on the above code sample, since there’s one interesting thing to stress: as you can see, it demonstrates how to display some useful information about one particular object, but you’re probably wondering if this same process can be performed with multiple objects.

    Fortunately, the answer is a resounding yes! And not only is it possible to carry out this task, but it’s extremely easy to do. Thus, in the next section, I’m going to teach you how to print, on the browser, data associated with the structure of different objects, including their methods and properties, by using the functionality brought by a single destructor.

    In order to learn the details of how this process will be performed, please visit the following section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery

    SunQuest




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