HomePHP PHP 5 Helpers: Calling Methods Out of Object Scope
PHP 5 Helpers: Calling Methods Out of Object Scope
Theoretically defining the role of helper classes in PHP 5 (and other popular server-side scripting languages) is a pretty approachable process. As their name suggests, helper classes provide developers with a set of logically-grouped methods that allow them to easily perform certain repetitive tasks that are common to different web applications. This is the second part of an eight-part article series that shows you how to build a variety of useful helper classes and expand on their functionality.
Typical examples of helper classes are those that dynamically build portions of a URL and validate incoming data, or that apply specific text-formatting filters to a supplied string before doing something useful with it, such as inserting it into a database table. Feel free to add your own to the list.
While everything looks good in theory when it comes to defining what a helper class is, things can get more complex when putting this definition into practice. This is especially true for inexperienced PHP programmers just starting to use the object-oriented paradigm for building web-based programs.
So, if you need to expand your existing skills as a PHP developer and wish to learn how to build helper classes with PHP 5 by means of a true hands-on approach, then begin reading this series of articles right now!
In the first part of this series, I offered a gentle introduction to creating a text helper class. This class was tasked with applying some basic filters to an inputted string. If you've already read that article, then itís quite probable that you have a clear idea of how to build this kind of class in a relatively easy manner.
The methods of the class that I just mentioned were declared implicitly dynamic, even though itís perfectly possible to call them statically, and the PHP engine wonít raise any errors about this process. However, it would be much better to declare these methods explicitly static, thus taking advantage of the functionality offered by the text helper class without having to spawn an instance of it.
Therefore, in this second part of the series Iím going to use a decent variety of code samples to explain how to improve the use of the text helper class developed before by turning its regular methods into static ones.
Want to see how this will be accomplished in a few basic steps? Then go ahead and start reading right away!