Inheritance and Polymorphism in PHP: Building a Form Generator - Part II - There is a long list in your life: listing the full code for each subclass (
Page 3 of 5 )
Let's get straight to the point. First, let's look at the subclass for input text objects. Here is its definition:
class inputTextObject extends formObject {
var $value;
var $maxlength;
function inputTextObject($label,$name,$style,$value,$maxlength){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
(is_int($maxlength)&&$maxlength>2&&$maxlenght<24)?
$this->maxlength=$maxlength:die('Invalid parameter
'.$maxlength);
}
function generateHTML(){
$html=$this->label.'<input type="text" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" maxlength="'.$this->maxlength.'" />';
return $html;
}
}
As you can deduce from the above listed code, the subclass extends the base class, adding two extra properties: $value and $maxlength. The constructor accepts the base class parameters, plus the additional ones, and calls the base class constructor for setting up the $label, $name and $style properties. That's very simple to understand. Also notice that the subclass exposes its own "generateHTML()" method, which generates the corresponding HTML for an text input element, according to its properties.
Now, I think that you have an approximate idea of how the rest of the subclasses will look. And certainly you're correct. They're similarly defined, with a few different properties, according to the type of form element treated, but all of them offer the "generateHTML()" method. So, here's the definition for a hidden field subclass:
class hiddenFieldObject extends formObject {
var $value;
function hiddenFieldObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html='<input type="hidden" name="'.$this->name.'"
value="'.$this->value.'" class="'.$this->style.'" />';
return $html;
}
}
In as similar way, we can define the subclass for a radio button element:
class radioButtonObject extends formObject {
var $value;
var $checked;
function radioButtonObject
($label,$name,$style,$value,$checked=''){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
($checked=='checked'||$checked=='')?$this-
>checked=$checked:die('Invalid parameter '.$checked);
}
function generateHTML(){
$html='<input type="radio" name="'.$this->name.'"
value="'.$this->value.'" class="'.$this->style.'"';
($this->checked=='checked')?$html.=' checked="'.$this-
>checked.'" />'.$this->label:$html.=' />'.$this->label;
return $html;
}
}
If building checkboxes is your favorite hobby (mine is building submit buttons), here is the checkbox definition:
class checkBoxObject extends formObject {
var $value;
var $checked;
function checkBoxObject($label,$name,$style,$value,$checked=''){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
($checked=='checked'||$checked=='')?$this-
>checked=$checked:die('Invalid parameter '.$checked);
}
function generateHTML(){
$html='<input type="checkbox" name="'.$this->name.'"
value="'.$this->value.'" class="'.$this->style.'"';
($this->checked=='checked')?$html.=' checked="'.$this-
>checked.'" />'.$this->label:$html.=' />'.$this->label;
return $html;
}
}
There are several subclasses to be defined. Don't forget them. Here are the select boxes:
class selectObject extends formObject {
var $size;
var $options;
function selectObject($label,$name,$style,$size,$options=array()){
parent::formObject($label,$name,$style);
(is_integer($size)&&$size>0)?$this->size=$size:die
('Invalid parameter '.$size);
(count($options)>0)?$this->options=$options:die
('Invalid parameter '.$options);
}
function generateHTML(){
$html=$this->label.'<select name="'.$this->name.'"
class="'.$this->style.'" size="'.$this->size.'">';
foreach($this->options as $option=>$value){
$html.='<option value="'.$value.'">'.$option.'</option>';
}
$html.='</select>';
return $html;
}
}
At this point, half of the job is done. Let's catch our breath, then continue defining the rest of the subclasses.