PHP
  Home arrow PHP arrow Page 3 - Expanding the Application Range of Vis...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Over the course of this second installment, I'll go deeper into the implementation...
       · I'm looking forward to this practical example.
       · Hello,The example has been included into the tutorial.Thank you.
       · In the last example, the lines: // visit 'SoftwareUser' object ...
       · Thank you for introducing your feedback regarding this article. Concerning your...
       · The comments by Anonymous Loozah at: 08-11-06 @ 5:03 am EST seem a bit harsh. ...
       · Hi Larry,First off, thank you for the compliments on this PHP article; I see you...
       · I agree with Larry, so want to thank Alejandro for writing these articles. It's very...
       · Hello Matthijs,Thank you for introducing your comments on my PHP article. In...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT