Helper classes seem easy to build -- almost deceptively so. If you're building a helper class to assist with a crucial function, however, such as validation, the approach you need to take may seem a bit less straightforward. This fifth article in an eight-part series on building helper classes walks you through the process, complete with hands-on examples.
True to form, for any PHP developer with only an average background in the object-oriented paradigm, building helper classes should be a pretty straightforward task. After all, this process doesnít demand the effort required for creating a full-blown library.
Itís fair to say, though, that even when a helper is designed to perform certain tasks that at least in theory shouldnít be extremely complex, in practice things can get a bit intimidating. The reason for this is simple: there are times when a helper class needs to implement methods or functions that play a role as crucial as a much larger module or plug-in.
Of course, speaking in practical terms, categorizing a particular class as a helper or as a core library depends strongly on the context in which it will be used. But regardless of its rather intangible nature, helpers are undeniably a fundamental part of a PHP developerís toolbox.
Therefore, if youíre interested in arming yourself with the right tools to start building your own helper classes in PHP 5 with minor efforts, then donít hesitate any longer; begin reading this series of articles right away!
Before you skip over this introduction, however, it would be helpful to recall that I left off the previous article discussing how to create a URL helper class whose main functionality was implemented around the $_SERVER PHP superblobal array. Despite the simplicity of many of its methods, the class demonstrated how to utilize it without having to deal with any instances of it, since the methods were declared static, and therefore used out of the object context.
Nonetheless, this journey through the ins and outs of building helper classes with PHP 5 hasnít finished yet. In this fifth part of the series I plan to build a class that will be responsible for validating input data, either supplied by users or from a different source.
In this specific project, this validation helper will make use of the PHP filter extension to perform the pertinent checking processes, but it can be easily adapted to use custom methods as well.
Now, itís time to leave the theory behind us and start learning how to create a helper class to validate input data. Letís go!