PHP
  Home arrow PHP arrow Page 3 - Expanding the Application Range of Visitor Objects in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Expanding the Application Range of Visitor Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2006-08-09


    Table of Contents:
  • Expanding the Application Range of Visitor Objects in PHP 5
  • Visiting users isn't a bad idea after all: setting up a new practical example
  • Deriving a subclass from the User base class: creating sets of software user objects
  • Visiting software users isn't boring at all: creating a concrete visitor class
  • Putting the classes to work together: seeing the visitor object in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Expanding the Application Range of Visitor Objects in PHP 5 - Deriving a subclass from the User base class: creating sets of software user objects
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT