PHP
  Home arrow PHP arrow Page 2 - Retrieving Information on Several Objects with 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? 
PHP

Retrieving Information on Several Objects with Destructors in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT