PHP
  Home arrow PHP arrow Page 5 - Introducing Visitor Objects in PHP 5
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

Introducing Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-08-02

    Table of Contents:
  • Introducing Visitor Objects in PHP 5
  • An introductory example
  • Defining another sample class
  • Defining the structure of a visitor
  • A 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


    Introducing Visitor Objects in PHP 5 - A visitor object in action


    (Page 5 of 5 )

    As I mentioned earlier, correctly understanding the behavior of a visitor object is a process that relies mostly on practical examples. That's exactly the reason why I coded a short script (shown below) which demonstrates the functionality of the visitor pattern. Look at the following code listing:

    try{
        // instantiate 'VisitedArray' object
        $visitedArray=new VisitedArray();
        // add some elements to the array
        $visitedArray->addElement('Element 1');
        $visitedArray->addElement('Element 2');
        $visitedArray->addElement('Element 3');
        $visitedArray->addElement('Element 4');
        $visitedArray->addElement('Element 5');
        // instantiate 'DataVisitator' object
        $dataVisitator=new DataVisitator();
        // accept visitor object
        $visitedArray->acceptVisitator($dataVisitator);
        $dataVisitator->visitArray($visitedArray);
        // display info about visited array
        echo '<p>Visitor object determined the following information
    about visited array:</p>';
       echo $dataVisitator->getArrayInfo();
       // instantiate 'VisitedFile' object
        $visitedFile=new VisitedFile();
        // add some lines to the file
        $visitedFile->addLine('This is line 1');
        $visitedFile->addLine('This is line 2');
        $visitedFile->addLine('This is line 3');
        $visitedFile->addLine('This is line 4');
        $visitedFile->addLine('This is line 5');
        // accept visitor object
        $visitedFile->acceptVisitator($dataVisitator);
        $dataVisitator->visitFile($visitedFile);
        // display info about visited file
        echo '<p>Visitor object determined the following information
    about visited file:</p>';
       echo $dataVisitator->getFileInfo();
    }

    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    Although the above script may seem simple at first glance, it shows in a nutshell the functionality of a visitor object. Notice how this object retrieves the size of the visited array, after populating it with some data, as well as how it obtains the size of the visited file once some strings have been added to this file.

    Of course, the output generated by the previous script is as following:

    Visitor object determined the following information about visited
    array:
    Visited array contains the following number of elements: 5

    Visitor object determined the following information about visited
    file:
    Visited file has a size of 600 bytes, and contains 5 lines

    Wasn't that great? After the corresponding visited classes neatly allowed the visitor object to inspect their properties, the corresponding information is outputted to the browser. Just let your mind go one step further and think about the implementation of a visitor object capable of inspecting all the visible properties of any visited classes. Here you have the foundations for doing that!

    Wrapping up

    Unfortunately, we've come to the end of the initial part of our discussion. In this first article of the series, I walked you through the basics of implementing the visitor pattern with PHP 5. I hope that all the code samples that you saw here will serve as an introduction to applying this pattern in a more sophisticated environment. From this moment on, it's up to you.

    In the second tutorial, I'll show you more practical examples of how to apply the visitor pattern with PHP 5. Stay tuned!


    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.

       · In this first installment of the series, you'll learn how to apply the visitor...
       · Alejandro,Thank you for writing such high quality material. I can't remember...
       · Hi Larry,Well, I guess this will be rather redundant but I thank you for all the...
     

       

    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 1 hosted by Hostway
    Stay green...Green IT