PHP
  Home arrow PHP arrow Page 4 - 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 - Putting the data checking system to work
    ( Page 4 of 4 )

    As I stated in the section that you just read, below I coded several hands-on examples that illustrate how the data validation system that you learned earlier works to check some basic input data.

    Here are the mentioned examples, along with their corresponding outputs:

    try{
       // create observer object
       $observer=new Observer;

       // create validator objects
       $alphaVal=new AlphaValidator;
       $numVal=new NumberValidator;
       $emailVal=new EmailValidator;

       // validate some basic input data
       $alphaVal->validate('This is a valid input string');
       $numVal->validate('This is not a number');
       $emailVal->validate('user@domain.com');
       if(!Observer::checkData()){
          echo 'Inputted data is not valid';
       }
       else{
          echo 'Inputted data is valid';
       }

       // displays the following:
       // Inputted data is not valid
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    //*******************************************
    try{
       // create observer object
       $observer=new Observer;

       // create validator objects
       $alphaVal=new AlphaValidator;
       $numVal=new NumberValidator;
       $emailVal=new EmailValidator;

       // validate some basic input data
       $alphaVal->validate('This is a valid input string');
       $numVal->validate(1);
       $emailVal->validate('user@domain');
       if(!Observer::checkData()){
          echo 'Inputted data is not valid';
       }
       else{
          echo 'Inputted data is valid';
       }

       // displays the following:
       // Inputted data is not valid
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    //*******************************************
    try{
       // create observer object
       $observer=new Observer;

       // create validator objects
       $alphaVal=new AlphaValidator;
       $numVal=new NumberValidator;
       $emailVal=new EmailValidator;

       // validate some basic input data
       $alphaVal->validate('1 is a number');
       $numVal->validate(1234);
       $emailVal->validate('user@domain.com');
       if(!Observer::checkData()){
          echo 'Inputted data is not valid';
       }
       else{
          echo 'Inputted data is valid';
       }

       // displays the following:
       // Inputted data is not valid
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    //*******************************************
    try{
       // create observer object
       $observer=new Observer;

       // create validator objects
       $alphaVal=new AlphaValidator;
       $numVal=new NumberValidator;
       $emailVal=new EmailValidator;

       // validate some basic input data
       $alphaVal->validate('S');
       $numVal->validate(1234);
       $emailVal->validate('user@domain.com');
       if(!Observer::checkData()){
          echo 'Inputted data is not valid';
       }
       else{
          echo 'Inputted data is valid';
       }

       // displays the following:
       // Inputted data is valid
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Weren't all the above code samples easy to follow? I'm sure they were. As you can see, the previous data checking mechanism uses the programmatic model imposed by the observer pattern, along with a single static property to validate different types of inputted data. Considering that coding the prior classes may take only a few minutes, and the system's capacity to be expanded upon, you'll have to agree with me that using static properties and methods with PHP 5 brings many benefits. 

    Final thoughts

    Sadly, we've come to the end of this series. Hopefully, after reading all these tutorials, you'll be better prepared to start using static data with your own PHP-driven applications.

    See you in the next PHP tutorial!



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek