HomePHP Creating a Static Web Form Helper with Restrictive Constructors
Creating a Static Web Form Helper with Restrictive Constructors
Welcome to the final installment of a series that shows you how to use restrictive constructors in PHP 5. With a decent variety of code samples, this series recreates for you a few common scenarios where the implementation of both protected and private constructors can greatly improve the behavior of different classes in the context of a given application.
And now that you've been properly introduced to the subject of this series, it’s time to briefly recapitulate the topics that were covered in the last tutorial. In that part of the series I built a basic MySQL abstraction class, which used a private constructor in conjunction with a static method called “getInstance()” to turn the class into a Singleton.
The functionality of this abstraction class was actually quite limited; it only was capable of running queries, fetching rows in data sets, and performing a few more basic tasks. Still, it came in handy for demonstrating how a private constructor can be of great help in implementing the Singleton design pattern in a strict fashion.
However, as I mentioned a moment ago, there are different situations where a restrictive constructor can be employed in a truly useful manner; one of them happens to be when building purely static classes. In most cases (not all of them, naturally), presentation helpers are good candidates for turning into static classes. In the lines to come I’m going to code one for you that will declare a private constructor.
In this particular case, the helper that I’m going to build will take care of rendering different elements of an HTML form in a pretty straightforward way, but it’ll be conceived to be used only out of the object context.
It sounds like a pretty educational experience, right? Thus, to learn the full details regarding the construction of this purely static web form helper, jump ahead and start reading the following lines!