PHP
  Home arrow PHP arrow Page 4 - Building a Data Validation System with the Prototype Pattern with 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

Building a Data Validation System with the Prototype Pattern with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2007-05-22


    Table of Contents:
  • Building a Data Validation System with the Prototype Pattern with PHP 5
  • Building a simple data validation system
  • Creating a few additional classes
  • Creating an example

  • 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


    Building a Data Validation System with the Prototype Pattern with PHP 5 - Creating an example
    ( Page 4 of 4 )

    As I said in the section that you just read, demonstrating the functionality of the prototype pattern, in this case implemented as part of a basic data validation application, is only a matter of building a simple PHP script that puts all the prototype classes to work together.

    Basically, what I'm going to illustrate here is that it's possible to work with a cloned instance of a specific validation object to check whether or not a particular input entry is considered offending. Naturally, the proper verification of incoming data will be performed by implementing the programmatic model dictated by the prototype pattern. In this way you can see more clearly how it works in this concrete case.

    All right, now that I have explained the purpose of coding a hands-on example, please pay attention to the following code listing. It shows this handy pattern in action. Here it is:   

    try{
       // create new 'AlphabeticValidatorPrototype' object
       $alphaPrototype=new AlphabeticValidatorPrototype();
       // clone prototype object
       $alphaVal=clone $alphaPrototype;
       // validate input data
       if(!$alphaVal->validateData('ABCDE')){
         echo 'Input data is not valid!';
       }
       else{
         echo 'Input data is valid!';
       }

       /* displays the following:
       Input data is valid!
       */

       // create new 'AlphanumericValidatorPrototype' object
       $alphanumPrototype=new AlphanumericValidatorPrototype();
       // clone prototype object
       $alphanumVal=clone $alphanumPrototype;
       // validate input data
       if(!$alphanumVal->validateData('****12345***')){
         echo 'Input data is not valid!';
       }
       else{
         echo 'Input data is valid!';
       }

       /* displays the following:
       Input data is not valid!
       */

       // create new 'NumericValidatorPrototype' object
       $numPrototype=new NumbericValidatorPrototype();
       // clone prototype object            
       $numVal=clone $numPrototype;
       // validate input data
       if(!$numVal->validateData('12345')){
         echo 'Input data is not valid!';
       }
       else{
         echo 'Input data is valid!';
       }

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

    Wasn't the above example easy to grasp? I bet it was! As you can see, the example first creates the corresponding prototype objects associated with every data checking class, and then uses the PHP built-in "clone()" method to create one instance of them. Next, these cloned instances perform a simple validation process on a bunch of primitive sample data, and finally an indicative message is displayed on the browser according to the results of this procedure.

    A final note before you finish reading this article: I recommend that you use all of the prototype classes that I included here as a starting point for understanding more clearly how the prototype pattern works. Of course, you may want to introduce your own modifications to the classes or even better, develop your own functional examples.

    As with many other topics associated specifically to PHP programming, it's the best way to learn. 

    Final thoughts

    Unfortunately, we've come to the end of this series. I hope that all the examples shown in this group of articles will be useful enough to demonstrate in a friendly fashion how the prototype pattern can be implemented with PHP 5.

    As you have seen, there are design patterns that are more unusual than others, and this is precisely the case with the prototype pattern. However, it doesn't hurt to learn at least its basic principles, and eventually how it can be used to solve specific programming-related issues.

    See you in the next PHP tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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