PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Building a simple data validation system
    ( Page 2 of 4 )

    As I said right in the beginning of this article, I'm going to demonstrate how to implement the prototype pattern by creating a simple mechanism for validating input data. As you'll see, this data checking application will be capable of working with many instances of a specific object, which will be used to verify the validity of certain data types, such as email addresses, alphanumeric and alphabetic values, etc.

    Okay, having explained how I plan to apply the prototype pattern in this particular situation, please have a look at the signatures of the following prototype classes. They establish a generic structure for validating alphanumeric and alphabetic strings.

    Here are the prototype classes:

    // define abstract 'DataValidatorPrototype' class
    abstract class DataValidatorPrototype{
       abstract public function validateData($inputData);
       abstract public function __clone();

    }

    // define concrete 'AlphabeticValidatorPrototype' class
    class AlphabeticValidatorPrototype extends DataValidatorPrototype{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
           return false;
         }
         return true;
       }
       public function __clone(){}
    }

    // define concrete 'AlphanumericValidatorPrototype' class
    class AlphanumericValidatorPrototype extends DataValidatorPrototype{
       public function validateData($inputData){
         if(!$inputData||!preg_match("/^[a-zA-Z0-9]+$/",$inputData)){
           return false;
         }
         return true;
       }
       public function __clone(){}
    }

    As illustrated above, the three classes are very convenient for checking certain types of data, and come in handy for working with alphabetic and alphanumeric values.

    Of course, you'll realize that the first abstract class, the one called "DataValidatorPrototype," is used to define the generic characteristics of any data validator object that might be potentially derived from it. However, aside from studying in detail the functionality provided by these classes, you should notice that each of them declares a magic "__clone()" method.

    This is very relevant, since the intrinsic definition of the prototype pattern says that all the objects spawned from the prototype will be created via a cloning process. Thus, that's exactly the reason for declaring the "__clone()" method. Quite simple, right?

    All in all, I defined two concrete classes for validating alphabetic and alphanumeric data. Considering that I'd like to extend the initial capacity of this data checking application, in the section to come I'm going to define a few additional prototype classes. They will be tasked with validating numbers and email addresses as well.

    To see how these brand new classes will be built, click on the link below and keep reading. 



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