Expanding the Application Range of Visitor Objects in PHP 5 - Putting the classes to work together: seeing the visitor object in action
(Page 5 of 5 )
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{
// instantiate 'SoftwareUser' object
$softwareUser=new SoftwareUser();
// set properties for SoftwareUser object
$softwareUser->setUserID(1);
$softwareUser->setFirstName('John');
$softwareUser->setLastName('Smith');
$softwareUser->setPostalAddress('1245 Binary Avenue NY');
//$softwareUser->setEmail('john@domain.com');
$softwareUser->setPreferredSoftware('PHP');
// instantiate 'SoftwareUserVisitor' object
$softwareUserVisitor=new SoftwareUserVisitor();
// accept visitor
$softwareUser->acceptVisitor($softwareUserVisitor);
// visit 'SoftwareUser' object
$softwareUserVisitor->visitSoftwareUser($softwareUser);
$userData=$softwareUserVisitor->getSoftwareUserInfo();
// display info about 'SoftwareUser' object via the visitor
foreach($userData as $key=>$data){
echo 'Value for '.$key.' is the following: '.$data.'<br />';
}
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
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
Value for firstname is the following: John
Value for lastname is the following: Smith
Value for postaladdress is the following: 1245 Binary Avenue NY
Value for software is the following: PHP
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |