Home arrow PHP arrow Page 4 - Introducing Visitor Objects in PHP 5

Defining the structure of a visitor - PHP

Although the article’s title may seem a bit intimidating, the truth is that things are much simpler than you think. Like many other programming languages, PHP also allows you to construct and use visitor objects with minor hassles. But, before I go deeper into the subject, first let’s ask ourselves the following question: what are visitor objects, after all?

  1. Introducing Visitor Objects in PHP 5
  2. An introductory example
  3. Defining another sample class
  4. Defining the structure of a visitor
  5. A visitor object in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
August 02, 2006

print this article



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: