PHP
  Home arrow PHP arrow Page 4 - 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 - 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


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