PHP
  Home arrow PHP arrow Page 4 - Using Static Methods to Validate Data with Helpers 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

Using Static Methods to Validate Data with Helpers in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-09-08


    Table of Contents:
  • Using Static Methods to Validate Data with Helpers in PHP 5
  • Review: checking incoming data with class methods
  • Declaring the class's methods to be static
  • Examples: validating input data with static methods

  • 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


    Using Static Methods to Validate Data with Helpers in PHP 5 - Examples: validating input data with static methods
    (Page 4 of 4 )

    If you're like me, then you wish to learn how to use the helper class, after having modified the declarations of its methods. Therefor, below I rewrote the examples shown in a previous section of this tutorial, which show how to validate different kinds of data by calling the methods in question statically. Here they are:

    // validate an integer value

    if (ValidatorHelper::validate_int(10.2, 1, 100) === FALSE)

    {

    echo 'Input value is not a valid integer.';

    }

    else

    {

    echo 'Input value is a valid integer.';

    }

    /*

    displays the following

    Input value is not a valid integer.

    */

     

    // validate a float value

    if (ValidatorHelper::validate_float(1234.5) === FALSE)

    {

    echo 'Input value is not a valid float number.';

    }

    else

    {

    echo 'Input value is a valid float number.';

    }

    /*

    displays the following

    Input value is a valid float number.

    */

     

    // validate an alphabetic value

    if (ValidatorHelper::validate_alpha('a1234') === FALSE)

    {

    echo 'Input value is not a valid alphabetic value.';

    }

    else

    {

    echo 'Input value is a valid alphabetic value.';

    }

    /*

    displays the following

    Input value is not a valid alphabetic value.

    */

     

    // validate an alphanumeric value

    if (ValidatorHelper::validate_alphanum('a1234') === FALSE)

    {

    echo 'Input value is not a valid alphanumeric value.';

    }

    else

    {

    echo 'Input value is a valid alphanumeric value.';

    }

    /*

    displays the following

    Input value is a valid alphanumeric value.

    */

     

    // validate a URL

    if (ValidatorHelper::validate_url('http://devshed.com') === FALSE)

    {

    echo 'Input value is not a valid URL.';

    }

    else

    {

    echo 'Input value is a valid URL.';

    }

    /*

    displays the following

    Input value is a valid URL.

    */

     

    // validate an IP address

    if (ValidatorHelper::validate_ip('127.0.0.1') === FALSE)

    {

    echo 'Input value is not a valid IP address.';

    }

    else

    {

    echo 'Input value is a valid IP address.';

    }

    /*

    displays the following

    Input value is a valid IP address

    */

     

    // validate an email address

    if (ValidatorHelper::validate_email('info@domain.com') === FALSE)

    {

    echo 'Input value is not a valid email address.';

    }

    else

    {

    echo 'Input value is a valid email address.';

    }

    /*

    displays the following

    Input value is a valid email address.

    */

    There you have it. Now the use of the methods that comprise the helper class looks much better, right? Admittedly, the development of this sample class isn't going to change the way that you currently build your PHP applications, but hopefully it'll help you grasp more easily the logic that surrounds the correct creation and usage of helpers.

    Finally, feel free to edit all of the code examples included in this article, which surely will contribute to sharpening your existing skills in working with static class methods and PHP filters.

    Final thoughts

    Sadly, we've come to the end of this series. I hope you enjoyed reading it as much as I did writing it. But most importantly, I believe that overall the experience has been instructive, since I provided you with an extensive explanation of how to correctly build helper classes in PHP 5.

    From this point on, you can use some of the approaches shown in the series and develop your own helper classes, which will save you from the annoyance of writing the same code over and over again.

    See you in the next PHP tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Getting More Info on Reflected Properties wi...
    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek