HomePHP Expanding the Application Range of Visitor Objects in PHP 5
Expanding the Application Range of Visitor Objects in PHP 5
Has your career as PHP developer led you to learn more about the most popular design patterns? If it has, then this series may help you get a better grounding in them. Welcome to the second installment of the series “Introducing Visitor Objects in PHP 5.” Made up of three articles, this series introduces the key points of how to apply the visitor pattern in PHP, and emphasizes the practical side of the topic by walking you though copious hands-on examples.
If the word "visitor" doesn't ring any bells to you, let me tell you quickly what it means in the context of pattern-based programming. Essentially, a visitor object is one that's passed in straightly to another by a specific method, in order to inspect its properties and retrieve relevant information about this visited object.
Since at first glance the theory about visitors can be sometimes pretty overwhelming, in the first tutorial of the series, I introduced an easy-to-follow example, which hopefully demonstrated by a friendly approach how to create visitors with PHP 5. The example in question showed how two data handling classes could be visited by a single visitor object, in order to obtain concrete data about them, without the need to invoke specifically any of its accessing methods.
Although the example shown in the previous article may not fit all the needs of a real-world application, I think that one of the most important things here consists of understanding the primary logic behind constructing visitor objects, and then, when the pattern has been appropriately mastered, it can be used in more realistic situations.
Well, at this stage I'm assuming that creating visitor objects with PHP isn't a strange concept to you any longer, therefore I'll continue this journey by developing some additional -yet useful- examples on how to build up and implement visitors with PHP 5. The main purpose of this is simply reaffirming the concepts deployed in the first tutorial, so you can have a solid background for including your own visitor classes inside your PHP applications.
With the preliminaries out of our way, let's move on and continue learning more examples on how to use the visitor pattern in PHP. Let's go!