PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Using a static property with the Observer pattern
    ( Page 3 of 4 )

    According to the concepts expressed in the previous section, my purpose here is simply to illustrate how a static property can be used to tackle a real world problem. I'm going to build a basic data validation mechanism for verifying common input types, like alphabetic and numeric data, as well as email addresses.

    Nonetheless, and here is where things get really interesting, the core logic of the system described above will be implemented via an observer class. As you might guess, this class will use a static property to control the validity of all the inputted data.

    Now that I have explained how this validation system will work, please take a look at the definition of the following classes, which are responsible for the correct implementation of the system in question:

    // define 'Observer' class
    class Observer{
       static public $errors=0;
       static public function checkData(){
          if(!self::$errors){
             return true;
          }
          return false;
       }
    }

    // define 'AlphaValidator' class
    class AlphaValidator extends Observer{
       public function validate($data){
          if(!preg_match("/^[a-zA-Z ]+$/",$data)){
             self::$errors++;
          }
       }
    }

    class NumberValidator extends Observer{
       public function validate($data){
          if(!is_numeric($data)){
             self::$errors++;
          }
       }
    }

    class EmailValidator extends Observer{
       public function validate($data){
          if(!preg_match("/^.+@.+..+$/",$data)){
             self::$errors++;
          }
       }
    }

    As show above, I coded some basic PHP classes, which in conjunction implement the so-called observer design pattern. Logically, in this particular case, the functionality provided by the pattern is utilized to validate basic types of input data. This process is performed by a bunch of specific data checking classes.

    However, you should notice that on top of this validation system, there's an observer class that uses a static property called "$errors" to determine whether any failures occurred when validating specific types of data. Of course, since this property is also shared by all the validation classes, its value is incremented each time a specific input data is considered invalid. In this case it "notifies the observer" that an error was triggered when validating a certain input. Pretty simple to grasp, right?

    All right, having explained the programming logic that drives the previous data checking system, it's time to move on and code an illustrative example. In this final example, all of the above classes will be put to work in conjunction, in this manner showing the actual functionality of the system.

    As you might guess, this practical demonstration will be done in the following section, thus click on the below link and keep reading.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT