PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - 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
     

       

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