In order to see how the two classes defined before can establish a mutual interaction, first I'll create an instance of the "SoftwareUser" class, and then the corresponding visitor object will be used for obtaining the values of all the properties exposed by the visited class. The code snippet that performs all the tasks that I mentioned is listed below. Check it out: try{ That was really easy, wasn't it? As you can see, after assigning some trivial values to the properties exposed by the respective "SoftwareUser" object, the visitor in question is responsible for visiting it and displaying all the available information about this object. With reference to the previous example, the last little thing to note is the usage of a "foreach" loop to output the complete set of values assigned to the properties of the "SoftwareUser" object. In order to complete the example, the result of this process is shown below: Value for userid is the following: 1 After seeing the result of the above script, don't say that implementing the visitor pattern in PHP 5 is hard work! To wrap up The second chapter of this journey is over. In this article I extended the application of the visitor pattern with PHP by walking you through another handy example, which demonstrated the correct implementation of this pattern. However, this trip hasn't finished yet. In the last part of the series, you'll learn how to use a visitor object to establish a programmatic link between different MySQL result sets and a pager class. You won't want to miss it!
blog comments powered by Disqus |