PHP
  Home arrow PHP arrow Page 4 - 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 - Validating user-supplied data by using polymorphic objects
    ( Page 4 of 4 )

    In accordance with the concepts that I deployed in the previous section, here is an illustrative example that shows how to use some of the data checking classes that you learned before. This will demonstrate how useful and powerful polymorphism can be.

    Take a look at the following code sample, please:

    try{
     
    $valFactory=new ValidatorFactory();
     
    // create 'EmptyValidator' object
     
    $emptyVal=$valFactory->createValidator('EmptyValidator');
     
    if($emptyVal->validate('')){
       
    echo 'Input data is valid!';
     
    }
     
    else{
       
    echo 'Input data is not valid!';
     
    }
     
    // create 'IntegerValidator' object
     
    $integerVal=$valFactory->createValidator('IntegerValidator');
     
    if($integerVal->validate(1000)){
       
    echo 'Input data is valid!';
     
    }
     
    else{
       
    echo 'Input data is not valid!';
     
    }
     
    // create 'NumericValidator' object
     
    $numericVal=$valFactory->createValidator('NumericValidator');
     
    if($numericVal->validate('ABCD')){
       
    echo 'Input data is valid!';
     
    }
     
    else{
       
    echo 'Input data is not valid!';
     
    }
     
    // create 'AlphanumericValidator' object
     
    $alphanumericVal=$valFactory->createValidator
    ('AlphanumericValidator');
     
    if($alphanumericVal->validate('ABCD123456')){
       
    echo 'Input data is valid!';
     
    }
     
    else{
       
    echo 'Input data is not valid!';
     
    }
     
    // create 'AlphabeticValidator' object
     
    $alphabeticVal=$valFactory->createValidator
    ('AlphabeticValidator');
     
    if($alphabeticVal->validate('ABCD')){
       
    echo 'Input data is valid!';
     
    }
     
    else{
       
    echo 'Input data is not valid!';
     
    }
    }
    catch(Exception $e){
     
    echo $e->getMessage();
     
    exit();
    }

    As you can see, the sample script shown above demonstrates in a friendly fashion how to use some of the classes created previously to validate different types of data. The respective objects display a primitive message indicating whether or not a particular value is valid, but this can be easily changed to fit your personal needs.

    As usual, feel free to tweak the source code of all the classes shown in this article, so you can eventually acquire a better understanding of how polymorphism can be used to build more efficient PHP applications.

    Final thoughts

    Finally, we've come to the end of this series. Hopefully, after reading these three tutorials, you'll have a more accurate idea of how to take advantage of the functionality provided by polymorphism to build more solid and robust object-based PHP applications.

    As expressed earlier, polymorphism can be applied successfully in those situations where you need to work with a bunch of objects that belong to the same family, but eventually can have different behaviors.

    See you in the next PHP tutorial!



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