Home arrow PHP arrow Page 5 - Expanding the Application Range of Visitor Objects in PHP 5

Putting the classes to work together: seeing the visitor object in action - PHP

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.

TABLE OF CONTENTS:
  1. Expanding the Application Range of Visitor Objects in PHP 5
  2. Visiting users isn't a bad idea after all: setting up a new practical example
  3. Deriving a subclass from the User base class: creating sets of software user objects
  4. Visiting software users isn't boring at all: creating a concrete visitor class
  5. Putting the classes to work together: seeing the visitor object in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
August 09, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: