// base class formObject
class formObject {
var $label;
var $name;
var $style;
function formObject($label,$name,$style){
(!is_numeric($label))?$this->label=$label:die('Invalid parameter '.$label);
(!is_numeric($name))?$this->name=$name:die('Invalid
parameter '.$name);
(!is_numeric($style))?$this->style=$style:die('Invalid
parameter '.$style);
}
function generateHTML(){
echo 'You are not overriding the generateHTML() method
of the formObject super class!';
}
}
// derived class inputTextObject
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;
}
}
// derived class hiddenFieldObject
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;
}
}
// derived class radioButtonObject
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;
}
}
// derived class checkBoxObject
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;
}
}
// derived class selectObject
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;
}
}
// derived class textAreaObject
class textAreaObject extends formObject {
var $value;
var $wrap;
function textAreaObject
($label,$name,$style,$value,$wrap='virtual'){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
($wrap=='off'||$wrap=='physical'||$wrap=='virtual')?
$this->wrap=$wrap:die('Invalid parameter '.$wrap);
}
function generateHTML(){
$html=$this->label.'<textarea name="'.$this->name.'"
class="'.$this->style.'" wrap="'.$this-
>wrap.'">'.$this->value.'</textarea>';
return $html;
}
}
// derived class buttonObject
class buttonObject extends formObject {
var $value;
function buttonObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="button" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
// derived class passwordObject
class passwordObject extends formObject {
var $maxlength;
function passwordObject($label,$name,$style,$maxlength){
parent::formObject($label,$name,$style);
(is_int($maxlength)&&$maxlength>8&&$maxlenght<24)?
$this->maxlength=$maxlength:die('Invalid parameter
'.$maxlength);
}
function generateHTML(){
$html=$this->label.'<input type="password"
name="'.$this->name.'" value="'.$this->value.'"
class="'.$this->style.'" />';
return $html;
}
}
// derived class fileObject
class fileObject extends formObject {
function fileObject($label,$name,$style){
parent::formObject($label,$name,$style);
}
function generateHTML(){
$html=$this->label.'<input type="file" name="'.$this-
>name.'" class="'.$this->style.'" />';
return $html;
}
}
// derived class imageObject
class imageObject extends formObject {
var $value;
var $src;
var $alt;
function imageObject
($label,$name,$style,$value,$src,$alt){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
(!is_numeric($alt))?$this->alt=$alt:die('Invalid
parameter '.$alt);
(file_exists($src))?$this->src=$src:die('Invalid
parameter '.$src);
}
function generateHTML(){
$html=$this->label.'<input type="image" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" src="'.$this->src.'" alt="'.$this-
>alt.'" />';
return $html;
}
}
// derived class resetObject
class resetObject extends formObject {
var $value;
function resetObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="reset" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
// derived class submitObject
class submitObject extends formObject {
var $value;
function submitObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="submit" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
Now you have some code to play around with. I hope you have a good time finding possible additions. Just be creative and put your background to work.
Over this three-part series, we've learned a bit more about the capabilities of Inheritance and Polymorphism in PHP 4, building a practical example to generate HTML forms using an Object-Oriented approach. Also, the process has introduced some theory concepts that hopefully will help you gain a better understanding of OOP. Without a doubt, the OOP approach is the best way to manage large projects in PHP, due to the pluggable nature of objects. When you have a couple of classes working for you, it's just a matter of making them interact to rapidly develop Web applications. The learning curve is rather long, but the effort is truly worthwhile.