Home arrow PHP arrow 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.

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

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:

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!';

}

}

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.



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