PHP
  Home arrow PHP arrow Page 2 - Using the Observer Design Pattern with Static Data 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? 
Google.com  
PHP

Using the Observer Design Pattern with Static Data in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek