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

Implementing the Form Generator: take one - 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

So far, we've seen the pieces of the form in separated classes. Having available the base "formObject" class, as well as the different derived sub classes, it's time to instantiate some form objects and build a basic HTML form. Our first approximation would be implemented sticking to the following approach:

// include required class file

require_once('formclasses.php');

// instantiate derived inputText objects from super class formObject

$textField1=&new inputTextObject('First
Name','firstname','textinput','',30);

$textField2=&new inputTextObject('Last
Name','lastname','textinput','',30);

// instantiate derived checkbox object from super class
formObject

$checkboxField=&new checkBoxObject('I want to receive
the weekly
newsletter.','newsletter','checkbox','newsletter');

// instantiate derived submit object from super class
formObject

$submitButton=&new submitObject
('','send','submitbutton','Send Data');

// make objects array

$objects=array
($textField1,$textField2,$checkboxField,$submitButton);

// generate form determining what type of object is
treated

foreach($objects as $object){

switch (get_class($object)){

case "inputtextobject":

echo $object->label.'<input name="'.$object->name.'"
type="text" maxlength="'.$object-
>maxlength.'" /><br />';

break;

case "checkboxobject":

echo '<input name="'.$object->name.'" type="checkbox"
value="'.$object->value.'" />'.$object->label.'<br />';

break;

case "radiobuttonobject":

echo '<input name="'.$object->name.'" type="radio"
value="'.$object->value.'" />'.$object->label.'<br />';

break;

case "submitobject":

echo $object->label.'<input name="'.$object->name.'"
type="submit" value="'.$object->value.'" /><br />';

break;

default:

break;

}

}

That's our rough approximation for coding our form generator, and I must say that is really bad! Even worse, it's dramatic. But, why are we complaining so loudly about the code? After all, we've been using the base class and subclasses, applying Inheritance,  and finally instantiating a few form objects, in order to build a regular HTML form. What could be so wrong? Just take a look at the section where we generate the form using an object array structure, and use a "switch" statement to determine which form object we're dealing with.

Utilizing the "get_class()" PHP built-in function, we're finding out what type of form element is contained in the array, and subsequently displaying the corresponding code for the element. Please forgive me about my vehemence, but switch statements are really poor and inefficient. Not only are we being redundant about HTML generation, but we're also accessing objects properties directly! What about encapsulation? Object properties must always be accessed by the set of objects' proper methods.

Also, the code is hiding a bigger problem. If we want to add another form element to the form generator, the updating process is really a nightmare. Definitely, this approach demonstrates how things must not be done.

So, what's our next step to fixing the above dirty implementation? The answer is Polymorphism. Yes, even in an extremely weakly typed language such as PHP, we can take advantage of it for finding a new approach to build the form generator. Do you remember that each subclass defined presents a "generateHTML()" method? So, why don't we get a bit smarter, using it to present a more decent solution? That's what we're going to put in practice in the last part of this series, demonstrating the big advantage of using polymorphic objects, and explaining its core concept definition.

Summary

In this second part of the series, we've finished defining the whole source code for each subclass corresponding to a form element. Also, hopefully we've demonstrated how poorly we could be implementing our form generator without using the advantages of Polymorphism as the previous step to fixing these major mistakes, employing its capabilities in PHP with a practical approach.

In the next part, we'll be putting Polymorphism to work for us, presenting a new version of our form generator. In the meantime, play with the code and add your own improvements to the project. After all, that's what collaborative work is about. See you in the third part!



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