HomePHP 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?
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(); }
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!