PHP
  Home arrow PHP arrow Page 5 - Expanding the Application Range of Vis...
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 
Mobile Linux 
App Generation ROI 
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

Expanding the Application Range of Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this second installment, I'll go deeper into the implementation...
       · I'm looking forward to this practical example.
       · Hello,The example has been included into the tutorial.Thank you.
       · In the last example, the lines: // visit 'SoftwareUser' object ...
       · Thank you for introducing your feedback regarding this article. Concerning your...
       · The comments by Anonymous Loozah at: 08-11-06 @ 5:03 am EST seem a bit harsh. ...
       · Hi Larry,First off, thank you for the compliments on this PHP article; I see you...
       · I agree with Larry, so want to thank Alejandro for writing these articles. It's very...
       · Hello Matthijs,Thank you for introducing your comments on my PHP article. In...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - 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





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