PHP
  Home arrow PHP arrow Page 2 - Using the Observer Design Pattern with...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Using the Observer Design Pattern with Static Data in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2007-09-17

    Table of Contents:
  • Using the Observer Design Pattern with Static Data in PHP 5
  • Handling user data via a single static property
  • Using a static property with the Observer pattern
  • Putting the data checking system to work

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using the Observer Design Pattern with Static Data in PHP 5 - Handling user data via a single static property


    (Page 2 of 4 )

    Before I continue demonstrating how useful a single static property can be in the context of object-oriented programming with PHP 5, I want to reintroduce the hands-on example developed in the preceding article of this series. This will make it fresh in your mind, which should really help if you want to reproduce the business logic implemented by it within your own PHP applications.

    As you probably recall, the example in question illustrated how a group of instances belonging to the same originating class can be easily linked with each other by using a single static property. The signatures corresponding each of the sample classes used for that specific occasion looked like this:

    // define abstract 'User' class - declares '$nextUserID' property
    static (it can be accessed by all the classes)
    abstract class User{
       static public $nextUserID=1;
       public $userID;
       abstract public function __construct($fname,$lname,$email);
       abstract public function getFirstName();
       abstract public function getLastName();
       abstract public function getEmail();
       abstract public function getID();
    }

    // define 'ForumUser' class
    class ForumUser extends User{
       public function __construct
    ($fname='John',$lname='Doe',$email='john@domain.com'){
          if(!$fname){
             throw new Exception('Invalid First Name for forum
    user.');
          }
          if(!$lname){
             throw new Exception('Invalid Last Name for forum
    user.');
          }
          if(!$email||preg_match("/^.+@.+..+$/",$email)){
             throw new Exception('Invalid email address for forum
    user.');
          }
          $this->fname=$fname;
          $this->lname=$lname;
          $this->email=$email;
          $this->userID=self::$nextUserID++;
       }

       // get user's First Name
       public function getFirstName(){
          return $this->fname;
       }

       // get user's Last Name
       public function getLastName(){
          return $this->lname;
       }

       // get user's email address
       public function getEmail(){
          return $this->email;
       }

       // get user's ID
       public function getID(){
          return $this->userID;
       }
    }
    try{
       // create some forum user objects
       $user1=new ForumUser('Robert','Wilson','bob@domain.com');
       $user2=new ForumUser('John','Smith','johnny@domain.com');
       $user3=new ForumUser('Susan','Jackson','suse@domain.com');
       $user4=new ForumUser('Mary','King','mary@domain.com');

       // display information on first forum user
       echo '<h2>Data for first forum user is as following:</h2>';
       echo '<p>First Name: '.$user1->getFirstName().'</p><p>Last
    Name: '.$user1->getLastName().'<p/><p>Email address: '.$user1-
    >getEmail().'</p><p>User ID: '.$user1->getID().'</p>';
       /*
       displays the following:
       
    Data for first forum user is as following:
       First Name: Robert
       Last Name: Wilson
       Email address: bob@domain.com
       User ID: 1
       */   

       // display information on second forum user
       echo '<h2>Data for second forum user is as following:</h2>';
       echo '<p>First Name: '.$user2->getFirstName().'</p><p>Last
    Name: '.$user2->getLastName().'<p/><p>Email address: '.$user2-
    >getEmail().'</p><p>User ID: '.$user2->getID().'</p>';
       /*
       displays the following:

    Data for second forum user is as following:
       First Name: John
       Last Name: Smith
       Email address: johnny@domain.com
       User ID: 2
       */

       // display information on third forum user
       echo '<h2>Data for third forum user is as following:</h2>';
       echo '<p>First Name: '.$user3->getFirstName().'</p><p>Last
    Name: '.$user3->getLastName().'<p/><p>Email address: '.$user3-
    >getEmail().'</p><p>User ID: '.$user3->getID().'</p>';
       /*
       displays the following:

    Data for third forum user is as following:
       First Name: Susan
       Last Name: Jackson
       Email address: suse@domain.com
       User ID: 3
       */

       // display information on last forum user
       echo '<h2>Data for last forum user is as following:</h2>';
       echo '<p>First Name: '.$user4->getFirstName().'</p><p>Last
    Name: '.$user4->getLastName().'<p/><p>Email address: '.$user4-
    >getEmail().'</p><p>User ID: '.$user4->getID().'</p>';
       /*
       displays the following:

    Data for last forum user is as following:
       First Name: Mary
       Last Name: King
       Email address: mary@domain.com
       User ID: 4
       */
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the previous sample classes implement a primitive application for displaying some basic data about a few fictional users, such as their first and last names, along with their email addresses. However, the most interesting thing to note here is the utilization of a single static "$nextUserID" property, which is used by all the instances of the originating "ForumUser" class to increment the ID of the next user. Indeed, this implementation of a given static property shows in a nutshell its remarkable functionality, particularly when it comes to enchaining several instances of a specific class.

    So far, so good. At this stage, you hopefully grasped the logic that drives the above hands-on example. So let's take a look at another case where the use of a single static property can be really handy.

    In this case, I'll show you how to build an expandable data validation system utilizing a basic static property in conjunction with the programmatic model dictated by the popular Observer design pattern.

    Want to see how this will be achieved? Jump ahead and read the next few lines.

    More PHP Articles
    More By Alejandro Gervasio


       · This final tutorial of the series explains how to use a static property shared by...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway