PHP
  Home arrow PHP arrow Page 5 - Introducing 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? 
PHP

Introducing Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      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


    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!



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT