Home arrow PHP arrow Page 3 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part I

The Base Class of the Form Generator - PHP

Object Oriented Programming (OOP) is a powerful technology for designing Web applications. This article is the first in a three part series that will explain two of the bases of OOP, Inheritance and Polymorphism, and apply these principles to building an extensible form generator.

TABLE OF CONTENTS:
  1. Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
  2. A Quick Look at Basic OOP Concepts
  3. The Base Class of the Form Generator
  4. Applying Inheritance: Creating Subclasses
By: Alejandro Gervasio
Rating: starstarstarstarstar / 50
April 05, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: