PHP
  Home arrow PHP arrow Page 4 - Building a Data Validation System 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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!


    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 last article of the series goes through the development of a data validation...
       · First I want to say that I enjoy reading your articles. They are clearly written...
       · Firstly, I’d like to say I’m glad you find my PHP articles useful, so I thank you...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway