Inheritance and Polymorphism in PHP: Building a Form Generator - Part I - The Base Class of the Form Generator (
Page 3 of 4 )
Having learned a bit more about Inheritance in PHP, we're going to establish the basics for our form generator. First, we should define a base class called "formObject," which sets the blueprints for any generic form element, that is, input text fields, radio buttons, checkboxes and the like. Doing so, whenever we need to create a new specific form element, by just deriving a new subclass from the base class, we can generate as many form elements as we want.
Let's start by defining the base class "formObject," which exposes three properties common to all of the form elements: a label, a name, and a style. Of course, we may add a few more properties, such as an ID, but for now we'll just keep it this way. This the first definition for the base class "formObject":
class formObject {
var $label;
var $name;
var $style;
function formObject($label,$name,$style){
// setup tasks performed here
}
}
As you can see, the base class is very simple. We have defined three common properties for it, $label, $name and $style, respectively. Then, as we usually do in PHP 4, the constructor is properly defined, accepting the three parameters previously mentioned. Correct me if I'm wrong, but this class is simply doing nothing. All right, let's extend the class definition to add more functionality to it. Here's the improved class:
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);
}
}
Now the class is beginning to look more appealing, since it's performing some useful initializing tasks. The constructor is now checking the validity of each possible parameter, before assigning them as class properties. We've decided to consider as valid arguments any non-numeric value, for being assigned to the properties $label, $name and $style. If any of them are numeric values, the class simply kills execution by calling the "die()" function.
As you can see, we're not being very restrictive about the possible values assigned to each property, only allowing them to be non-numeric values. The reason for doing this is that we simply don't want any label value looking like 12120, or names such as -12.8, included in the HTML markup. You get the idea. But since the code is easily extensible, you might add your own validation rules for incoming parameters.
So far, the class is setting up the $label, $name and $style properties, which are common to each possible form element. Now, we need to define a method for generating the corresponding HTML code for every form object. So, let's be more specific here. Since we're going to use this base class for creating all of the derived subclasses, the method for building the HTML should be generic, displaying a warning message, so that it won't be overridden on each subclass. We'll see it more clearly in detail later. For now, let's define the generic method "generateHTML()" in the following way:
function generateHTML(){
echo 'You are not overriding the generateHTML() method of the formObject super class!';
}
This last method finally structures our base class "formObject" as follows:
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!';
}
}
That's fairly good. Our base class "formObject" is finally finished and it's doing a nice job, because it is defining the common properties for the future derived subclasses that will build each specific form element. Please don't feel disappointed about the class code. Were you expecting hundreds of lines? Remember that we're going to implement the power of Inheritance in PHP to create the subclasses for radio buttons, checkboxes, input text elements and even more. Thus, let's take a look at the structure of each derived subclass.
| | Discuss Inheritance and Polymorphism in PHP: Building a Form Generator - Part I | | | | | | | Eve the subject has been treated many times on books and articles, the article is... | | | | | | That's a really awkward place to stop Part I. If you didn't have time to finish the... | | | | | | Uh, please pardon the tone of my post above. You made it quite clear in the summary... | | | | | | Your apologies are accepted. No problem about that. Even my schedule is pretty... | | | | | | In trying to understand all that was said I am a little confused about some things... | | | | | | Well I have some expirience programing php and I would like to suggest that in many... | | | | | | Sorry, I forgot to include a sample of the “alternative syntax” I was referring to.... | | | | | | Yes, you're correct. Sometimes when you have many parameters to be passed to the... | | | | | | Hi friend,
I guess you're refering to the ternary operator. Well, it's an easy way... | | | | | | Hi there,
I guess you're talking about the ternary operator. It's often used to... | | | | | | The usage of the :: syntax is the following. You can call a function defined in an... | | | | | | Thanx for the great tutorial, I've used in some way OO programing in my scripts but... | | | | | | Hi dear friend,
I'm glad you found this tutorial useful for your growing... | | | | | | Is there any reason why u decided to use PHP4 and its object syntax? Wouldn't it be... | | | | | | Despite the fact that PHP5 offers much better support for OOP, their usage is still... | | | | | | Thanks for sharing us your very nice article!
jen | | | | | | Thank you for your compliments about the article. Your comments are deeply... | | | | | | >>> Post your comment now! | | | | | |
|
 |