PHP
  Home arrow PHP arrow Page 5 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part II
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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 II
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 25
    2005-04-12


    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part II
  • That refreshing touch: core definition for base class and subclasses
  • There is a long list in your life: listing the full code for each subclass
  • Coming up: more subclasses
  • Implementing the Form Generator: take one

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 II - Implementing the Form Generator: take one
    ( Page 5 of 5 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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