PHP
  Home arrow PHP arrow Page 2 - Validating Incoming Data by Using Polymorphism with Objects 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? 
PHP

Validating Incoming Data by Using Polymorphism with Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-04-04


    Table of Contents:
  • Validating Incoming Data by Using Polymorphism with Objects in PHP 5
  • Using polymorphism to validate input data
  • Extending the implementation of polymorphism
  • Validating user-supplied data by using polymorphic objects

  • 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


    Validating Incoming Data by Using Polymorphism with Objects in PHP 5 - Using polymorphism to validate input data
    ( Page 2 of 4 )

    A good place to start demonstrating how a group of polymorphic classes can be used to validate user-supplied data is with building a base abstract class. This class will define only one generic method, in this case called "validate()," and aimed at validating different types of input data.

    After defining this parent class, I'll derive a few concrete subclasses which will implement concretely the aforementioned "validate()" method, so each of them can be used to validate specific types of incoming data. Now, do you see how polymorphism fits into this schema? I'm sure you do!

    All right, now that I have explained how I plan to create a data validation application using some polymorphic classes, please take a look at the signature of the corresponding parent, which as I said before, has been declared abstract. Its short definition is as follows:

    // define 'DataValidator' class
    abstract class DataValidator{
       abstract public function validate($inputData);
    }

    As you can see, the above "DataValidator" class acts simply as an interface for creating all the corresponding subclasses that will be responsible for validating specific types of incoming data. In this case in particular, notice that the class only exposes an abstract "validate()" method, but this should give you at least a vague idea of how the classes that will created later on will implement concretely the method in question.

    Okay, now that you know how the base class looks, it's time to start deriving some concrete classes from it so they're able to validate different types of user-supplied data.

    Having said that, here are the definitions that correspond to the first four subclasses. They are tasked with checking empty strings first, then integers and numeric data, and finally ranges of values.

    The respective signatures for these brand new classes are as follows:

    // define 'EmptyValidator' class
    class EmptyValidator extends DataValidator{
      
    const MIN=4;
      
    const MAX=32;
      
    public function validate($inputData){
        
    if(!$inputData||trim($inputData)==''||strlen($inputData)
    <self::MIN||strlen($inputData)>self::MAX){
          
    return false;
        
    }
        
    return true;
       }
    }

    // define 'IntegerValidator' class
    class IntegerValidator extends DataValidator{
      
    public function validate($inputData){
        
    if(!$inputData||!is_numeric($inputData)||intval
    ($inputData)!=$inputData){
          
    return false;
        
    }
        
    return true;
      
    }
    }

    // define 'NumericValidator' class
    class NumericValidator extends DataValidator{
      
    public function validate($inputData){
        
    if(!$inputData||!is_numeric($inputData)){
          
    return false;
        
    }
        
    return true;
      
    }
    }

    // define 'RangeValidator' class
    class RangeValidator extends DataValidator{
      
    const MIN=1;
      
    const MAX=99;
      
    public function validate($inputData){
        
    if(!$inputData||$inputData<self::MIN||$inputData>self::MAX){
          
    return false;
        
    }
        
    return true;
      
    }
    }

    As shown above, the data validating classes listed previously are an excellent example of how to use the functionality provided by polymorphism to check a broad range of incoming data. This concept becomes clear since all the prior classes belong to the same "DataValidator" family, but in each case they implement the "validate()" method in a different way. Pretty good, isn't it?

    Okay, at this point I'm positive that you already grasped the logic that stands behind this "polymorphic" data validation system, so it's time to move forward and continue defining some additional subclasses. In this way the initial capacity of the system in question can be greatly expanded.

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



     
     
    >>> 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