Home arrow PHP arrow Page 4 - Using Static Methods to Validate Data with Helpers in PHP 5

Examples: validating input data with static methods - PHP

Welcome to the conclusion of an eight-part series on building helpers in PHP 5. It's been a long journey, modifying our helper class to make it do what we want it to. Finally, in this last part, we'll put the finishing touches on our validation class so that it functions properly, without the need to spawn helper objects.

TABLE OF CONTENTS:
  1. Using Static Methods to Validate Data with Helpers in PHP 5
  2. Review: checking incoming data with class methods
  3. Declaring the class's methods to be static
  4. Examples: validating input data with static methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
September 08, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: