PHP
  Home arrow PHP arrow Page 4 - 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 - Defining the structure of a visitor
    ( Page 4 of 5 )

    Until now, you learned how to build some classes that are capable of accepting a visitor object, which comes in handy for retrieving information about these classes. Nevertheless, the structure of a visitor class still remains undefined. This is a strong motivation for illustrating how a visitor object can be created. I begin this process by creating its abstract signature, and then by deriving some subclasses.

    That said, here is the structure of a highly generic visitor class. Please, examine the code listed below:

    // define abstract 'Visitator' class
    abstract class Visitator{
        abstract function visitArray(VisitedArray $visitedArray);
        abstract function visitFile(VisitedFile $visitedFile);
    } 

    As you can see, this entirely new abstract class represents the generic model of a visitor object, and exposes the two corresponding abstract methods, to visit the pair of classes that were defined in the previous section.

    At this stage, if you study in detail the definition of the above class, you'll realize how a visitor visits the pertinent target classes, since its two methods accept them as input arguments. Indeed, the relationship between objects here is very comprehensible.

    All right, now that you know how an abstract visitor looks, it's time to derive at least one child visitor, in such a way that it can implement the prior abstract methods. Regarding this point, below is the definition of a concrete visitor class:

    // define 'DataVisitator' class
    class DataVisitator extends Visitator{
        private $arrayInfo;
        private $fileInfo;
        // get info about visited array
       public function visitArray(VisitedArray $visitedArray){
            $this->arrayInfo='Visited array contains the following
    number of elements: '.$visitedArray->getSize();
        }
        // return info about visited array
        public function getArrayInfo(){
            return $this->arrayInfo;
        }
        // get info about visited file
        public function visitFile(VisitedFile $visitedFile){
            $this->fileInfo='Visited file has a size of
    '.$visitedFile->getSize().' bytes, and contains '.$visitedFile-
    >getNumLines().' lines';
        }
        // return info about visited file
        public function getFileInfo(){
            return $this->fileInfo;
        }
    }

    As you can see, on this occasion I created a concrete visitor class, which I called "DataVisitator." This class offers a concrete implementation of the "visitArray()" and "visitFile()" methods respectively, to obtain information about the size of the visited array in question, as well as get the dimension of the pertinent visited file.

    At this point, it should be clear to you how a visitor can "inspect" the properties of the visited objects, and retrieve relevant information about them. After all, implementing the visitor pattern with PHP 5 isn't difficult at all, but I believe this article would be rather incomplete if I didn't show you a hands-on example that uses all the classes that I defined before.

    Speaking of that, over the next few lines, you'll learn how the corresponding visitor object collects data that belongs to the visited classes. As usual, see you in the next section.



     
     
    >>> 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