PHP
  Home arrow PHP arrow Page 4 - Decoupling the Validation of Data with Bridge Classes 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

Decoupling the Validation of Data with Bridge Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-01-10


    Table of Contents:
  • Decoupling the Validation of Data with Bridge Classes in PHP 5
  • Building a bridge validator class
  • Defining a set of bridged classes
  • Putting all the classes to work

  • 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


    Decoupling the Validation of Data with Bridge Classes in PHP 5 - Putting all the classes to work
    ( Page 4 of 4 )

    As you’ll recall from the section that you just read, in this final part of this article I’m going to set up a practical example that shows how to use all the classes that were previously created to validate different types of user-supplied data.

    Indeed, this sounds really interesting and hopefully the code samples that you’ll see in a moment will help you grasp more easily how the bridge pattern can be implemented with a few simple PHP 5 classes.

    Having said that, here is the corresponding set of try-catch blocks, which demonstrate the correct usage of each of the data validation classes that were built in previous sections, and the “BridgeDataValidator” class as well. Have a look at these examples, please:

    // example to validate strings try{ // instantiate 'BridgeDataValidator' object $bdatVal=new BridgeDataValidator(array(1,2,3,4),
    'The supplied input data must be a string!','string'); // validate string data $bdatVal->validate(); /* displays the following: The supplied input data must be a string! */ } catch(Exception $e){ echo $e->getMessage(); exit(); } // example to validate numbers try{ // instantiate 'BridgeDataValidator' object $bdatVal=new BridgeDataValidator('This is not a number',
    'The supplied input data must be a number!','number'); // validate numeric data $bdatVal->validate(); /* displays the following: The supplied input data must be a number! */ } catch(Exception $e){             echo $e->getMessage();             exit(); } // example to validate alphabetic values try{ // instantiate 'BridgeDataValidator' object $bdatVal=new BridgeDataValidator(1234,'The supplied input data

    must be an alphabetic value!','alpha'); // validate alphabetic data $bdatVal->validate(); /* displays the following: The supplied input data must be an alphabetic value! */ } catch(Exception $e){ echo $e->getMessage(); exit(); } // example to validate an email address try{ // instantiate 'BridgeDataValidator' object $bdatVal=new BridgeDataValidator('user@fakedomain.com',
    'The supplied input data must be a valid email address!','email'); // validate email address $bdatVal->validate(); /* displays the following: The supplied input data must be a valid email address! */ } catch(Exception $e){ echo $e->getMessage(); exit(); }


    All right, I think that the group of code samples that were shown above are more than enough for demonstrating the correct implementation of the bridge design pattern in PHP 5. In addition, you should notice how in each case I provided the “BridgeDataValidator” object with an invalid input parameter, a condition that obviously throws the corresponding exception and also makes the script display the respective error message.

    As I have said with many other articles that I wrote for the prestigious Developer Shed network, don’t hesitate to introduce your own modifications to all the sample classes that were shown here. This will help you to acquire a more intimate knowledge of how the bridge pattern works. It’s a really educational experience, trust me!

     Final thoughts

    Unfortunately, we’ve come to the end of this article. However, I hope this tutorial has been instructive for you, specifically with reference to applying the bridge pattern as part of a more complex data validation application. As you saw, the process is really straightforward and also educational.

    However, this journey is not over yet. In the last part of the series, I’m going to teach you how to use the mentioned pattern to build an abstraction layer for working with MySQL. See you in the final article!



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