PHP
  Home arrow PHP arrow Page 4 - Inheritance and Polymorphism in PHP: B...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2005-04-19

    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part III
  • Turning back time: that old inefficient script
  • Polymorphism: the core definition
  • Implementing the Form Generator: take two
  • Listing full source code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Inheritance and Polymorphism in PHP: Building a Form Generator - Part III - Implementing the Form Generator: take two


    (Page 4 of 5 )

    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


       · In this final article of the series, I expose the core concepts about Polymorphism,...
       · Hi,Thanks for the article, very cool. I am new to php and trying to learn sort...
       · Thanks for the comments about the article.In fact, integrating this set of classes...
       · This is a great series of articles! Two questions though:1) Is there any way you...
       · Hello Adams,Thank you for the comments about the article. They're...
       · Here´s the full source code for each...
       · Hello Alejandro. First, thanks very much for the many interesting tutorials. A...
       · Hello,Thanks for the compliments on my PHP articles, and I really appreciate...
       · Thanks very much Alejandro. It works.Now I need to go back and compare this with...
       · I'm glad to know that all the classes I listed here were useful to get the script...
       · Hi again Alejandro.I see what my problem was. I got stuck at the step with...
       · Hello again,I'm feeling glad of knowing that the problems has been solved. Also,...
       · Dear Alejandro,Thanks for the tutorials on php to which I'm new. Having cut and...
       · Hi Vince,Thank you for the kind comments on my PHP article. Now, concerning your...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT