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 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 - Coming up: more subclasses


    (Page 4 of 5 )

    If you think that the above class definitions are not enough for building our form generator, you're right. There are still more form elements to be defined. So, it's time to continue defining more form subclasses. The next one to be defined is a <textarea> element, listed below:

    class textAreaObject extends formObject {

    var $value;

    var $wrap;

    function textAreaObject($label,$name,$style,$value,$wrap='virtual'){

    parent::formObject($label,$name,$style);

    (!is_numeric($value))?$this->value=$value:die('Invalid
    parameter '.$value);

    ($wrap=='off'||$wrap=='physical'||$wrap=='virtual')?
    $this->wrap=$wrap:die('Invalid parameter '.$wrap);

    }

    function generateHTML(){

    $html=$this->label.'<textarea name="'.$this->name.'"
    class="'.$this->style.'" wrap="'.$this-
    >wrap.'">'.$this->value.'</textarea>';

    return $html;

    }

    }

    The above listed class for building a <textarea> element accepts two additional parameters: $value and $wrap, in order to use them for proper HTML generation. In this case, we've given a default value of "virtual" for the $wrap parameter.

    Now let's look at the form button elements. Its class definition is the following:

    class buttonObject extends formObject {

    var $value;

    function buttonObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    (!is_numeric($value))?$this->value=$value:die('Invalid
    parameter '.$value);

    }

    function generateHTML(){

    $html=$this->label.'<input type="button" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    Our next step consists of listing the class for generating password fields. Here's the code:

    class passwordObject extends formObject {

    var $maxlength;

    function passwordObject($label,$name,$style,$maxlength){

    parent::formObject($label,$name,$style);

    (is_int($maxlength)&&$maxlength>8&&$maxlenght<24)?
    $this->maxlength=$maxlength:die('Invalid parameter
    '.$maxlength);

    }

    function generateHTML(){

    $html=$this->label.'<input type="password"
    name="'.$this->name.'" value="'.$this->value.'"
    class="'.$this->style.'" />';

    return $html;

    }

    }

    Okay, let's put in a bit more effort, and define the class for file uploads elements:

    class fileObject extends formObject {

    function fileObject($label,$name,$style){

    parent::formObject($label,$name,$style);

    }

    function generateHTML(){

    $html=$this->label.'<input type="file" name="'.$this-
    >name.'" class="'.$this->style.'" />';

    return $html;

    }

    }

    Let's not forget to define the subclass for reset buttons. Here is the code:

    class resetObject extends formObject {

    var $value;

    function resetObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    (!is_numeric($value))?$this->value=$value:die('Invalid
    parameter '.$value);

    }

    function generateHTML(){

    $html=$this->label.'<input type="reset" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    Finally, we're going to show our last two classes: form image elements and submit buttons. First, here's the code for form image elements, often used for submitting forms without using the venerable submit button:

    class imageObject extends formObject {

    var $value;

    var $src;

    var $alt;

    function imageObject
    ($label,$name,$style,$value,$src,$alt){

    parent::formObject($label,$name,$style);

    (!is_numeric($value))?$this->value=$value:die('Invalid
    parameter '.$value);

    (!is_numeric($alt))?$this->alt=$alt:die('Invalid
    parameter '.$alt);

    (file_exists($src))?$this->src=$src:die('Invalid
    parameter '.$src);

    }

    function generateHTML(){

    $html=$this->label.'<input type="image" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" src="'.$this->src.'" alt="'.$this-
    >alt.'" />';

    return $html;

    }

    }

    Finally, the class definition for a submit button:

    class submitObject extends formObject {

    var $value;

    function submitObject($label,$name,$style,$value){

    parent::formObject($label,$name,$style);

    (!is_numeric($value))?$this->value=$value:die('Invalid
    parameter '.$value);

    }

    function generateHTML(){

    $html=$this->label.'<input type="submit" name="'.$this-
    >name.'" value="'.$this->value.'" class="'.$this-
    >style.'" />';

    return $html;

    }

    }

    I must give a short disclaimer: notice that in all of the subclasses that accept a $value parameter, we've opted not to consider as a valid argument any numeric value. While this validation process could be leaving out some potentially valid parameters, we've chosen to do this based on a data type checking rather than performing validation on data length. For particular cases, just implement the validation process you believe best suits your needs.

    Now we have fully defined each subclass derived from the form base class for creating the form generator. Since subclasses nicely expose the "generateHTML()" method, it is easy to figure out a technique to put all of the subclasses to work and build a complete HTML form. So, let's try creating a regular form, using the above defined classes.

    More PHP Articles
    More By Alejandro Gervasio


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