PHP
  Home arrow PHP arrow Page 3 - 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 - There is a long list in your life: listing the full code for each subclass


    (Page 3 of 5 )

    Let's get straight to the point. First, let's look at the subclass for input text objects. Here is its definition:

    class inputTextObject extends formObject {

    var $value;

    var $maxlength;

    function inputTextObject($label,$name,$style,$value,$maxlength){

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

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

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

    }

    function generateHTML(){

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

    return $html;

    }

    }

    As you can deduce from the above listed code, the subclass extends the base class, adding two extra properties: $value and $maxlength. The constructor accepts the base class parameters, plus the additional ones, and calls the base class constructor for setting up the $label, $name and $style properties. That's very simple to understand. Also notice that the subclass exposes its own "generateHTML()" method, which generates the corresponding HTML for an text input element, according to its properties.

    Now, I think that you have an approximate idea of how the rest of the subclasses will look. And certainly you're correct. They're similarly defined, with a few different properties, according to the type of form element treated, but all of them offer the "generateHTML()" method. So, here's the definition for a hidden field subclass:

    class hiddenFieldObject extends formObject {

    var $value;

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

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

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

    }

    function generateHTML(){

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

    return $html;

    }

    }

    In as similar way, we can define the subclass for a radio button element:

    class radioButtonObject extends formObject {

    var $value;

    var $checked;

    function radioButtonObject
    ($label,$name,$style,$value,$checked=''){

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

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

    ($checked=='checked'||$checked=='')?$this-
    >checked=$checked:die('Invalid parameter '.$checked);

    }

    function generateHTML(){

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

    ($this->checked=='checked')?$html.=' checked="'.$this-
    >checked.'" />'.$this->label:$html.=' />'.$this->label;

    return $html;

    }

    }

    If building checkboxes is your favorite hobby (mine is building submit buttons), here is the checkbox definition:

    class checkBoxObject extends formObject {

    var $value;

    var $checked;

    function checkBoxObject($label,$name,$style,$value,$checked=''){

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

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

    ($checked=='checked'||$checked=='')?$this-
    >checked=$checked:die('Invalid parameter '.$checked);

    }

    function generateHTML(){

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

    ($this->checked=='checked')?$html.=' checked="'.$this-
    >checked.'" />'.$this->label:$html.=' />'.$this->label;

    return $html;

    }

    }

    There are several subclasses to be defined. Don't forget them. Here are the select boxes:

    class selectObject extends formObject {

    var $size;

    var $options;

    function selectObject($label,$name,$style,$size,$options=array()){

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

    (is_integer($size)&&$size>0)?$this->size=$size:die
    ('Invalid parameter '.$size);

    (count($options)>0)?$this->options=$options:die
    ('Invalid parameter '.$options);

    }

    function generateHTML(){

    $html=$this->label.'<select name="'.$this->name.'"
    class="'.$this->style.'" size="'.$this->size.'">';

    foreach($this->options as $option=>$value){

    $html.='<option value="'.$value.'">'.$option.'</option>';

    }

    $html.='</select>';

    return $html;

    }

    }

    At this point, half of the job is done. Let's catch our breath, then continue defining the rest of the subclasses.

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