PHP
  Home arrow PHP arrow Page 3 - Validating Incoming Data by Using Poly...
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

Validating Incoming Data by Using Polymorphism with Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    Validating Incoming Data by Using Polymorphism with Objects in PHP 5 - Extending the implementation of polymorphism


    (Page 3 of 4 )

    As I stated in the section that you just read, below I included a new group of data checking classes, created specifically for validating alphabetic and alphanumeric values, as well as email addresses. You should notice that I also created an additional class that verifies the validity of a given email address that can be used with Windows-based machines, in case you're working with that operating system.

    Having clarified that, here are the signatures that correspond to the aforementioned classes. They look like this:

    // define 'AlphabeticValidator class
    class AlphabeticValidator extends DataValidator{
       public function validate($inputData){
        
    if(!$inputData||!preg_match("/^[a-zA-Z]+$/",$inputData)){
          
    return false;
        
    }
        
    return true;
      
    }
    }

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

    // define 'EmailValidator' class
    class EmailValidator extends DataValidator{
      
    public function validate($inputData){
        
    if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
    checkdnsrr(array_pop(explode("@",$inputData)),"MX")){
          
    return false;
        
    }
        
    return true;
       
    }
    }

    // define 'EmailValidatorWin' class
    class EmailValidatorWin extends DataValidator{
      
    public function validate($inputData){
        
    if(!$inputData||!preg_match("/.+@.+..+./",$inputData)||!
    $this->windnsrr(array_pop(explode("@",$inputData)),"MX")){
          
    return false;
        
    }
        
    return true;
      
    }
      
    private function windnsrr($hostName,$recType=''){
        
    if($hostName){
          
    if($recType=='')$recType="MX";
          
    exec("nslookup -type=$recType $hostName",$result);
          
    foreach($result as $line){
            
    if(preg_match("/^$hostName/",$line)){
              
    return true;
            
    }
          
    }
          
    return false;
         
    }
        
    return false;
      
    }
    }

    As you can see, the above data validation classes look very similar to the ones created in the previous section. Logically, the only difference to spot here is with reference to the diverse implementations of the respective "validate()" method. It's different for each class.

    Again, you'll realize that I'm playing with the concept of polymorphism to expand the capacity of this data checking application, something that you can try for yourself if you want to verify additional types of data. As you learned previously, the process is indeed simple.

    Well, at this stage I have built a decent number of data checking classes for validating a broad range of user-supplied entries by taking advantage of polymorphism. However, there's still a missing piece in this schema, since a programmatic mechanism is required here to demonstrate more clearly how to use the polymorphic facet exposed by all these classes.

    So, what is that piece? In short, I plan to use a factory class so I can easily spawn a certain number of validating objects. These objects will be used to check different types of user-supplied data. Of course, I'll keep the source code of this factory class extremely simple, because I don't want to bother you with irrelevant details. Here's how this class looks:

    // define 'ValidatorFactory' class
    class ValidatorFactory{
      
    public function createValidator($validator){
        
    return new $validator();
       
    }
    }

    The above factory class returns to client code a specific kind of validating object, in accordance with the incoming $validator parameter passed to its respective "createValidator()" method.

    So far, so good. At this point, I have shown you the complete signatures that correspond to all the validation classes that you learned before, in conjunction with an additional factory class. Therefore, the next step will consist of developing a hands-on example, where you'll see how all these classes can be put to work together.

    Want to see how this practical example will be developed? Keep reading, please.

    More PHP Articles
    More By Alejandro Gervasio


       · In this final installment of the series you’ll learn how to develop an expandable...
       · I dont get why you make a separate class for every validator? Why not make 1 class ...
       · Thank you for commenting on my PHP article. I really appreciate your...
     

       

    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