PHP
  Home arrow PHP arrow Page 3 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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: 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
     
     
    ADVERTISEMENT


    Retrieving Information on Several Objects with Destructors in PHP 5 - Working with different user handling objects


    (Page 3 of 4 )

    In accordance with the concepts that I expressed in the previous section, I intend to demonstrate how to use the same “User” class that you saw earlier to retrieve information on multiple user objects, instead of only one.

    First, I’m going to list the signature of the aforementioned “User” class, so you can more easily recall how it looks. Here it is:


    // 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>';

    }

    }

    }


    Since the above user handling class was discussed previously, I’m not going to waste your valuable time explaining how it works. Instead, I’ll do something slightly more useful and show you how to use this class to create three brand new user objects, and then display their methods and properties with the assistance of their respective destructors.

    To perform this interesting task successfully, I’m going to use the following PHP 5 script:


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

    }


    As you can see, the above script simply creates three objects that store information about some fictional users, and then, by way of the pertinent methods, this data is printed straight to the browser. Nothing to complex to grasp, right?

    Nevertheless, as you may guess, there’s something else happening behind the scenes, since when the script finishes running, obviously it calls each of the destructors of the objects in question. That is a very interesting process. In this case, only one class method is utilized to retrieve data about multiple objects, which demonstrates in a nutshell how useful a destructor can be when adequately implemented.

    However, I do want you to digest all of this material in small bits, so in the last section of this tutorial I’m going to show you the output produced by the prior script. Thus I suggest you take a deep breath and read the next few lines.

    More PHP Articles
    More By Alejandro Gervasio


     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





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