HomePHP Page 2 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part II
That refreshing touch: core definition for base class and subclasses - 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.
Since the subclasses can quite easy be entirely defined, as you'll see in a moment, before looking at their whole source code and logic it would be useful to refresh our memory of the skeletal structure for at least a couple of them. To begin with, let's show the code for the base class, as it was originally defined:
echo 'You are not overriding the generateHTML() method of the formObject super class!';
}
}
The above base class is really acting as the core structure for defining any further form element. It offers three common properties ($label, $name and $style) shared by any form element, being inherited in the derived subclasses. It also offers the "generateHTML()" method, which, as we'll see shortly, will take care of generating the proper HTML for each type of form object (text inputs, radio buttons, and so forth).
Because each subclass is rather defined in a similar fashion, let's remember the basic definitions for the input text and the radio button objects, just to make sure that we're heading the right way for building the form generator. The basic code for the subclass tied to a text input element looked like this:
// class definition for input text object
class inputTextObject extends formObject {
var $value;
var $maxlength;
function inputTextObject($label,$name,$style,$value,$maxlength){
parent::formObject($label,$name,$style);
// properties setup
}
function generateHTML(){
}
}
And, for radio buttons, the code was the following:
// class definition for radio button object
class radioButtonObject extends formObject {
var $value;
var $checked;
function radioButtonObject($label,$name,$style,$value,$checked=''){
parent::formObject($label,$name,$style);
// properties setup
}
function generateHTML(){
}
}
We're not going to list the basic code for each subclass tied to a form element again! That was already done in the previous article. However, demonstrating how they looked in their original incarnation is the perfect foundation for moving forward and seeing the complete code for each subclass.
If you ever have worked with HTML forms (and who hasn't?), then you know that they offer numerous elements. It seems that the list for all of the subclasses is rather lengthy. If you don't take my word for it, take a look at the following section.