Home arrow PHP arrow Page 3 - 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 - PHP

In part two of this three-part series, we refresh our memory of Inheritance and subclasses from part one, and take our first stab at implementing a form generator.

TABLE OF CONTENTS:
  1. Inheritance and Polymorphism in PHP: Building a Form Generator - Part II
  2. That refreshing touch: core definition for base class and subclasses
  3. There is a long list in your life: listing the full code for each subclass
  4. Coming up: more subclasses
  5. Implementing the Form Generator: take one
By: Alejandro Gervasio
Rating: starstarstarstarstar / 28
April 12, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: