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?
Well, if you've been using design patterns for a while in your PHP applications, then you've probably heard about the Visitor pattern. If you haven't yet, let me give you a short description of what this pattern does. In simple terms, when it's applied appropriately, the pattern allows you to build up objects that literally visit other objects to obtain information about the target objects. This process is usually done by inputting the visitors into the visited objects via a concrete method.
To put it simply way, suppose that you have a bunch of objects that represent, in the software universe, the profiles of several users. This example looks pretty familiar, doesn't it? Well, it's possible to create a set of objects that "visit" these users and obtain certain information related to them, such as First and Last Name, user IDs, email addresses, and so forth. The exciting aspect of this situation is that this entire process can be performed programmatically, by using some simple classes, and as you know, PHP makes working with objects a no-brainer task.
So that's a small portion of theory of building visitor objects. Nevertheless, you'll agree with me that one of the most interesting aspects of design patterns is precisely their practical side. Therefore, considering this undeniable fact, in conjunction with your permanent wish for extending your background in object-oriented programming with PHP, over the course of this series I'll be showing you some useful examples of how to build visitor objects, and more specifically how to include them in real-world applications.
Assuming that creating visitor objects with PHP has already caught your attention, let's introduce ourselves to the subject and start learning more about it.