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

Deriving a subclass from the User base class: creating sets of software user objects - 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

As I said before, after defining the overall structure of the "User" class, I'll derive a subclass from it to implement concretely all its abstract methods. In this case, because I'm usually inclined to work with software packages, the child class that I plan to derive will be called "SoftwareUser."

Please look at the signature of this new class:

//define concrete 'SoftwareUser' class (extends User class)
class SoftwareUser extends User{
    private $software;
    // set user ID
    public function setUserID($userID){
        if(!userID||!is_int($userID)||$userID<0){
            throw new Exception('Invalid userID!');
        }
        $this->userID=$userID;
    }
    // get user ID
    public function getUserID(){
        return $this->userID;
    }
    // set user's first Name
    public function setFirstName($firstName){
        if(!$firstName||strlen($firstName)<4||strlen($firstName)
>32){
            throw new Exception('Invalid First Name (it must be a
string between 8 and 32 characters long.)');
        }
        $this->firstName=$firstName;
    }
    // get user's first Name
    public function getFirstName(){
        return $this->firstName;
    }
    // set user's last name
    public function setLastName($lastName){
        if(!$lastName||strlen($lastName)<4||strlen($lastName)>32)
{
            throw new Exception('Invalid Last Name (it must be a
string between 4 and 32 characters long.)');
        }
        $this->lastName=$lastName;
    }
    // get user's Last Name
    public function getLastName(){
        return $this->lastName;
    }
    // set user's postal address
    public function setPostalAddress($postalAddress){
        if(!$postalAddress||strlen($postalAddress)<8||strlen
($postalAddress)>64){
            throw new Exception('Invalid Postal Address (it must
be a string between 4 and 64 characters long.)');
        }
        $this->postalAddress=$postalAddress;
    }
    // get user's postal address
    public function getPostalAddress(){
        return $this->postalAddress;
    }
    // set user's email
    public function setEmail($email){
        if(!$email||!preg_match("/.+@.+..+./",$email)||!
checkdnsrr(array_pop(explode("@",$email)),"MX")){
            throw new Exception('Invalid Email Address');
        }
        $this->email=$email;
    }
    // get user's email address
    public function getEmail(){
        return $this->email;
    }
    // set user's preferred software
    public function setPreferredSoftware($software){
        if(!$software){
            throw new Exception('Invalid software name');
        }
        $this->software=$software;
    }
    // get user's preferred software
    public function getPreferredSoftware(){
        return $this->software;
    }
    // accept visitor
    public function acceptVisitor(Visitor $visitor){
        $visitor->visitSoftwareUser($this);
    }
}

As illustrated above, the "SoftwareUser" class now offers a concrete implementation for all the abstract methods that were defined originally inside the base class. Nevertheless, aside from looking into the set of accessors and modifiers, I want you to pay attention to the following method:

public function acceptVisitor(Visitor $visitor){
    $visitor->visitSoftwareUser($this);
}

If you recall the concepts deployed in the first article, then you'll realize that the above method applies a nearly identical implementation to the examples shown in that particular tutorial to accept a visitor object. This schema will be almost the same each time you need to code a visited class, therefore I recommend that you keep it fresh in your mind.

So far, I demonstrated how to create a "SoftwareUser" class which is provided with the ability to accept a visitor object which will eventually inspect the pertinent properties. However, things get really exciting when coding the respective visitor class.

I'm pretty sure that you want to see how this class will look, therefore click on the link that appears below and keep learning more.



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