Home arrow PHP arrow Page 4 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part II

Coming up: more 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

If you think that the above class definitions are not enough for building our form generator, you're right. There are still more form elements to be defined. So, it's time to continue defining more form subclasses. The next one to be defined is a <textarea> element, listed below:

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;

}

}

The above listed class for building a <textarea> element accepts two additional parameters: $value and $wrap, in order to use them for proper HTML generation. In this case, we've given a default value of "virtual" for the $wrap parameter.

Now let's look at the form button elements. Its class definition is the following:

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;

}

}

Our next step consists of listing the class for generating password fields. Here's the code:

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;

}

}

Okay, let's put in a bit more effort, and define the class for file uploads elements:

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;

}

}

Let's not forget to define the subclass for reset buttons. Here is the code:

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;

}

}

Finally, we're going to show our last two classes: form image elements and submit buttons. First, here's the code for form image elements, often used for submitting forms without using the venerable submit button:

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;

}

}

Finally, the class definition for a submit button:

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;

}

}

I must give a short disclaimer: notice that in all of the subclasses that accept a $value parameter, we've opted not to consider as a valid argument any numeric value. While this validation process could be leaving out some potentially valid parameters, we've chosen to do this based on a data type checking rather than performing validation on data length. For particular cases, just implement the validation process you believe best suits your needs.

Now we have fully defined each subclass derived from the form base class for creating the form generator. Since subclasses nicely expose the "generateHTML()" method, it is easy to figure out a technique to put all of the subclasses to work and build a complete HTML form. So, let's try creating a regular form, using the above defined classes.



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