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

Implementing the Form Generator: take two - PHP

In the last part of this three-part series of articles, we will find out how to improve the form generator we created in part II by learning about Polymorphism and adding it to the mix.

TABLE OF CONTENTS:
  1. Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
  2. Turning back time: that old inefficient script
  3. Polymorphism: the core definition
  4. Implementing the Form Generator: take two
  5. Listing full source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
April 19, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

With the pillars of Inheritance and Polymorphism playing in our favor, it's time to define the final version of our so-called form generator. Since each form element subclass derived from the base class exposes a specific "generateHTML()" method, the process for implementing Polymorphism is fairly straightforward. All that we need to do is get rid of any "switch" statements and take advantage of the capabilities present in the subclasses. Doing so, our revamped form generator should be coded in the following way:

// 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);

$textField3=&new inputTextObject('Email','email','textinput','Enter your@email',30);

// instantiate derived checkbox object from super clas formObject

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

// instantiate derived selectbox object from super class formObject

$selectBox1=&new selectObject('Occupation','occupation','selectbox',1,array('Web Programmer'=>'programmer','Web Designer'=>'webdesigner','Graphic Designer'=>'graphicdesigner'));

// instantiate derived text area object

$textArea=&new textAreaObject('Comments','comment','','Enter comments here');

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

// instantiate derived submit object from super class formObject

// make objects array

$objects=array($textField1,$textField2,$textField3,$checkBox1,
$selectBox1,$textArea,$submitButton);

// generate form using polymorphism

foreach($objects as $object){

echo $object->generateHTML().'<br /><br />';

}

Whew! The code definitely looks a lot better. We simply instantiated a set of three of text input objects, a checkbox, and a select menu. Then we added a text area and, finally, a regular submit button.

With all of the form objects properly defined, we've created an array structure and looped through the objects, just invoking the "generateHTML()" method in each iteration. In this way we generated the complete HTML form. Simple enough, eh?

As you can see, this method is by far more efficient and maintainable. With a few changes on the base class, all of the subclasses would automatically inherit the new properties or methods. In a similar way, if we need to add another form element to the form generator, just adding the corresponding subclass, we get the job done nicely.

And that's all for now…oh, wait a minute! A few final disclaimers are needed. Taking a deeper look at the code for each form object subclass, we clearly see that they offer limited possibilities for defining the overall layout for different form elements. However, this could be changed, easily adding an ID property to tie a style, or redefining the "generateHTML()" method, for major layout flexibility. I've shown a basic form layout. Now you can take the code, reuse it, expand it, or create your own version of it. The possibilities are vast.

My final observation points to application performance. Since we're working possibly with numerous objects here, in slow systems this might cause an extra overload for the Web server. Having said that, we're finally done. For those who want to include this solution in Web applications, we'll list the complete source code, including the base class and its subclasses.



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