PHP
  Home arrow PHP arrow Page 5 - 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 II
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this second part, the article defines the source code for each derived subclass...
       · Lately I read part One and must say i very similar to the last one. I think you...
       · I'm sorry you feel that way. Maybe I should have join the two parts in one section....
       · I just wanted to put a note of appreciation for your tutorial so far. In the past...
       · Thank you for the kind words about the article. I see you're working on a future...
       · Alejandro,I was writing a similar class to what you're describing here.. many...
       · Thank you for your positive opinions about the tutorial. They're very...
     

       

    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 6 hosted by Hostway
    Stay green...Green IT