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

A visitor object in action - 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?

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: