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!