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

Visiting software users isn't boring at all: creating a concrete visitor class - 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

It's quite possible that you're wondering how a visitor class can be coded to allow all its properties to be retrieved without having to use its accessing methods. Well, fortunately your question can be answered in two easy steps: first, I'll define the generic structure of a visitor class, and then I'll create a subclass from it.

Indeed, this sounds logical, thus in accordance with this approach, below I listed the signature of the abstract "Visitor" class:

// define abstract 'Visitor' class
abstract class Visitor{
    abstract function visitSoftwareUser(SoftwareUser $softwareUser);
} 

That's all I need to define the generic model of a visitor. Notice the declaration of the abstract "visitSoftwareUser()" method, which will accept as input argument the visited object, in this case represented by an instance of the "SoftwareUser" class.

Now that you know how the prior abstract class looks, it's time to implement its visiting method. That said, here is the corresponding definition of the entirely new "SoftwareUserVisitor" subclass:

// define concrete 'SoftwareUserVisitor' class
class SoftwareUserVisitor extends Visitor{
    private $softwareUserInfo=array();
    // obtain user information as array
    public function visitSoftwareUser(SoftwareUser $softwareUser){
        $this->softwareUserInfo['userid']=$softwareUser-
>getUserID();
        $this->softwareUserInfo['firstname']=$softwareUser-
>getFirstName();
        $this->softwareUserInfo['lastname']=$softwareUser-
>getLastName();
        $this->softwareUserInfo['postaladdress']=$softwareUser-
>getPostalAddress();
        //$this->softwareUserInfo['email']=$softwareUser-
>getEmail();
        $this->softwareUserInfo['software']=$softwareUser-
>getPreferredSoftware();
    }
    public function getSoftwareUserInfo(){
        return $this->softwareUserInfo;
    }
} 

As you can see, now this brand new class offers a concrete implementation for its "visitSoftwareUser()" method. It's clear to see here how all the properties of an object of type "SoftwareUser" are retrieved by the visitor in question, and additionally, to make things even easier, they're stored in an array. Short and understandable!

At this level, after defining the two classes that allow the implementation of the visitor pattern, that is, the visited and visitor classes respectively, it's a good time to watch them together in action.

Not surprisingly, that's exactly the subject that I'll cover in the next few lines. Keep reading; I'll be waiting for you in the following section.



 
 
>>> 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: