PHP
  Home arrow PHP arrow Page 5 - Expanding the Application Range of Visitor Objects 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

Expanding the Application Range of Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2006-08-09


    Table of Contents:
  • Expanding the Application Range of Visitor Objects in PHP 5
  • Visiting users isn't a bad idea after all: setting up a new practical example
  • Deriving a subclass from the User base class: creating sets of software user objects
  • Visiting software users isn't boring at all: creating a concrete visitor class
  • Putting the classes to work together: seeing the visitor object 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


    Expanding the Application Range of Visitor Objects in PHP 5 - Putting the classes to work together: seeing the visitor object in action
    ( Page 5 of 5 )

    In order to see how the two classes defined before can establish a mutual interaction, first I'll create an instance of the "SoftwareUser" class, and then the corresponding visitor object will be used for obtaining the values of all the properties exposed by the visited class.

    The code snippet that performs all the tasks that I mentioned is listed below. Check it out:

    try{
        // instantiate 'SoftwareUser' object
        $softwareUser=new SoftwareUser();
        // set properties for SoftwareUser object
        $softwareUser->setUserID(1);
        $softwareUser->setFirstName('John');
        $softwareUser->setLastName('Smith');
        $softwareUser->setPostalAddress('1245 Binary Avenue NY');
        //$softwareUser->setEmail('john@domain.com');
        $softwareUser->setPreferredSoftware('PHP');
        // instantiate 'SoftwareUserVisitor' object
        $softwareUserVisitor=new SoftwareUserVisitor();
        // accept visitor
        $softwareUser->acceptVisitor($softwareUserVisitor);
        // visit 'SoftwareUser' object
        $softwareUserVisitor->visitSoftwareUser($softwareUser);
        $userData=$softwareUserVisitor->getSoftwareUserInfo();
        // display info about 'SoftwareUser' object via the visitor
        foreach($userData as $key=>$data){
            echo 'Value for '.$key.' is the following: '.$data.'<br />';
        }
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    That was really easy, wasn't it? As you can see, after assigning some trivial values to the properties exposed by the respective "SoftwareUser" object, the visitor in question is responsible for visiting it and displaying all the available information about this object.

    With reference to the previous example, the last little thing to note is the usage of a "foreach" loop to output the complete set of values assigned to the properties of the "SoftwareUser" object. In order to complete the example, the result of this process is shown below:

    Value for userid is the following: 1
    Value for firstname is the following: John
    Value for lastname is the following: Smith
    Value for postaladdress is the following: 1245 Binary Avenue NY
    Value for software is the following: PHP

    After seeing the result of the above script, don't say that implementing the visitor pattern in PHP 5 is hard work!

    To wrap up

    The second chapter of this journey is over. In this article I extended the application of the visitor pattern with PHP by walking you through another handy example, which demonstrated the correct implementation of this pattern.

    However, this trip hasn't finished yet. In the last part of the series, you'll learn how to use a visitor object to establish a programmatic link between different MySQL result sets and a pager class. You won't want to miss it!



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