HomePHP Page 2 - Controlling Online Forms with Builder Objects in PHP 5
Defining a form element class - PHP
Mastering some of the most popular design patterns with PHP 5 can be sometimes an overwhelming process that requires hard work and experience. However, if you want to tackle the challenge and expand your existing background on them, this article might eventually find a place on your quick reference list.
In order to start demonstrating how the builder pattern can be used for constructing online forms, first I need to define a target object that can be "manipulated" by the corresponding director and builder objects.
First I'll define an abstract "FormElement" class and then I'll derive a child from it, which will offer a concrete implementation for all its abstract methods. That said, here is the initial definition of the "AbstractFormElement" class:
// define abstract 'AbstractFormElement' class
abstract class AbstractFormElement{
abstract function getHTML();
}
If you were thinking that creating an abstract form element class with PHP was a hard-to-perform process, then I believe you'll find that you're wrong. As usually, the previous class defines the generic structure of a typical form control, and also presents an abstract method called "getHTML()," which will be responsible for returning to calling code the appropriate (X)HTML markup that corresponds to the online form in question. As you can see, this is nothing unexpected.
Now, let me take one step forward and create a child class from the "AbstractFormElement" class defined earlier. The signature of this new sub class is listed below, thus examine its source code:
// define concrete 'formElement' class
class FormElement extends AbstractFormElement{
private $html='';
public function __construct($type='text',$attributes=array
('name'=>'default'),$options=array()){
// check for <input> elements
if(preg_match("/^(text|radio|checkbox|password|
hidden|submit|reset|button|image|file)$/",$type)){
$openTag='<input type="'.$type.'" ';
$closeChar=' ';
$closeTag='/>';
}
// check for <textarea> and <select> elements
else if(preg_match("/^(textarea|select)$/",$type)){
$openTag='<'.$type.' ';
$closeChar='>';
$closeTag='</'.$type.'>';
}
else{
throw new Exception('Invalid form element
type');
}
if(!is_array($attributes)||count($attributes)<1){
throw new Exception('Invalid number of
attributes for <'.type.'> element');
}
//loop over element attributes
$elemAttributes='';
foreach($attributes as $attribute=>$value){
if(!$attribute||!$value){
throw new Exception
('Invalid attribute or value for <'.type.'> element');
}
$elemAttributes.=$attribute.'="'.$value.'" ';
}
//check for <select> options
$selOptions='';
if(count($options)>0){
foreach($options as $value=>$text){
if(!$value||!$text){
throw new Exception
('Invalid value or text for <'.type.'> element'
}
$selOptions.
='<option value="'.$value.'">'.$text.'</option>';
}
}
// build form element(X)HTML output
$this->html.=$openTag.trim($elemAttributes).
$closeChar.$selOptions.$closeTag;
}
// return overall (X)HTML output
public function getHTML(){
return $this->html;
}
}
As illustrated above, now the new child "FormElement" class offers a concrete implementation for each of the methods declared in the base class. Of course, the most important thing to note here concerns all the tasks performed inside the constructor.
As you can appreciate, this method really does all the hard work required for creating the (X)HTML markup that corresponds to each element of a regular online form, based on the pair of arrays supplied as input arguments. Finally, the respective form's code is retrieved by the tight "getHTML()" method. Simple and powerful!
Well, now that you know how the target object (in this case referenced by the "FormElement" class) has been correctly defined, the next step consists of creating the corresponding builder class, in this way introducing the second component required by the builder pattern (remember that the third one is the director).
In the next section I'll be defining the mentioned builder class, therefore I recommend you click on the below link and keep on reading. See you there.