PHP
  Home arrow PHP arrow Page 4 - 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 
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

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


    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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