HomePHP Page 3 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class
A smarter factory: applying Polymorphism within the “formElementFactory” class - PHP
In this third part of the series, I will explain how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects. This will boost the functionality of the factory class by simplifying the process for rendering form elements.
Essentially, making the form element factory a little “smarter” implies boosting it with the ability to exploit the polymorphic nature of form objects. Since each one of these elements presents the same “getHTML()” method, it’s extremely easy to encapsulate it within the class structure and return the specific (X)HTML code.
Considering this, here is the improved version of the form element factory class:
abstract class formElementFactory{ private function __construct(){} public static function createElement($type,$attributes=array()){ if(!class_exists($type)||!is_array($attributes)){ throw new Exception('Invalid method parameters'); } // instantiate a new form element object $formElement=new $type($attributes); // return form object’s HTML return $formElement->getHTML(); } }
Quite simple, right? Now, the class is capable of directly returning the (X)HTML code that corresponds to each form object. The overall form generation process is now a little more efficient, reduced to something like this:
// make array with parameters to be passed to the class $formElements=array('textinput'=>array ('name'=>'username','value'=>'','maxlength'=>'20'), 'passwordinput'=>array ('name'=>'password','value'=>'','maxlength'=>'20'), 'submitbutton'=>array ('name'=>'name','value'=>'Send')); foreach($formElements as $element=>$attributes){ // display form elements echo formElementFactory::createElement($element,$attributes).'<br />'; }
Now the code looks stronger and more compact. What I’ve done is simply built a recursive array with the parameters to be passed to the class, in order to display a couple of input fields (a text box and a password field), and a submit button. Finally, the form is roughly displayed, by calling the static “createElement()” method within a “foreach” loop.
To have a better idea of the functionality of the boosted factory class, here is the crude code for displaying several form elements:
// display an input text box echo formElementFactory::createElement('textinput',array ('name'=>'name','value'=>'','maxlength'=>'20'));
// display a password field echo formElementFactory::createElement('passwordinput',array ('name'=>'name','value'=>'','maxlength'=>'20'));
// display a hidden field echo formElementFactory::createElement('hiddeninput',array ('name'=>'name','value'=>'default'));
// display a submit image echo formElementFactory::createElement('imageinput',array ('name'=>'name','src'=>'submit.gif'));
// display a file upload field echo formElementFactory::createElement('fileinput',array ('name'=>'name','value'=>'1'));
// display a radio button echo formElementFactory::createElement('radiobutton',array ('name'=>'name','value'=>'1','checked'=>'true'));
// display a text area echo formElementFactory::createElement('textarea',array ('name'=>'name','value'=>'fill this field'));
// display a check box echo formElementFactory::createElement('checkbox',array ('name'=>'name','value'=>'1','checked'=>'true'));
// display a button echo formElementFactory::createElement('button',array ('name'=>'name','value'=>'button'));
// display a submit button echo formElementFactory::createElement('submitbutton',array ('name'=>'name','value'=>'Send'));
// display a reset button echo formElementFactory::createElement('resetbutton',array ('name'=>'name','value'=>'Reset'));
// display a select box echo formElementFactory::createElement('selectbox',array ('name'=>'name','size'=>'1','options'=>array ('1'=>'value1','2'=>'value2')));
Of course, this is an extremely ineffective implementation of the factory class, but it is useful to have a set of illustrative examples covering how each form element is rendered.