PHP
  Home arrow PHP arrow Page 3 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
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? 
Google.com  
PHP

Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 40
    2005-04-05


    Table of Contents:
  • Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
  • A Quick Look at Basic OOP Concepts
  • The Base Class of the Form Generator
  • Applying Inheritance: Creating Subclasses

  • 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 I - The Base Class of the Form Generator
    ( Page 3 of 4 )

    Having learned a bit more about Inheritance in PHP, we're going to establish the basics for our form generator. First, we should define a base class called "formObject," which sets the blueprints for any generic form element, that is, input text fields, radio buttons, checkboxes and the like. Doing so, whenever we need to create a new specific form element, by just deriving a new subclass from the base class, we can generate as many form elements as we want.

    Let's start by defining the base class "formObject," which exposes three properties common to all of the form elements: a label, a name, and a style. Of course, we may add a few more properties, such as an ID, but for now we'll just keep it this way. This the first definition for the base class "formObject":

    class formObject {

    var $label;

    var $name;

    var $style;

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

    // setup tasks performed here

    }

    }

    As you can see, the base class is very simple. We have defined three common properties for it, $label, $name and $style, respectively. Then, as we usually do in PHP 4, the constructor is properly defined, accepting the three parameters previously mentioned. Correct me if I'm wrong, but this class is simply doing nothing. All right, let's extend the class definition to add more functionality to it. Here's the improved class:

    class formObject {

    var $label;

    var $name;

    var $style;

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

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

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

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

    }

    }

    Now the class is beginning to look more appealing, since it's performing some useful initializing tasks. The constructor is now checking the validity of each possible parameter, before assigning them as class properties. We've decided to consider as valid arguments any non-numeric value, for being assigned to the properties $label, $name and $style. If any of them are numeric values, the class simply kills execution by calling the "die()" function.

    As you can see, we're not being very restrictive about the possible values assigned to each property, only allowing them to be non-numeric values. The reason for doing this is that we simply don't want any label value looking like 12120, or names such as -12.8, included in the HTML markup. You get the idea. But since the code is easily extensible, you might add your own validation rules for incoming parameters.

    So far, the class is setting up the $label, $name and $style properties, which are common to each possible form element. Now, we need to define a method for generating the corresponding HTML code for every form object. So, let's be more specific here. Since we're going to use this base class for creating all of the derived subclasses, the method for building the HTML should be generic, displaying a warning message, so that it won't be overridden on each subclass. We'll see it more clearly in detail later. For now, let's define the generic method "generateHTML()" in the following way:

    function generateHTML(){

    echo 'You are not overriding the generateHTML() method of the formObject super class!';

    }

    This last method finally structures our base class "formObject" as follows:

    class formObject {

    var $label;

    var $name;

    var $style;

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

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

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

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

    }

    function generateHTML(){

    echo 'You are not overriding the generateHTML() method of the formObject super class!';

    }

    }

    That's fairly good. Our base class "formObject" is finally finished and it's doing a nice job, because it is defining the common properties for the future derived subclasses that will build each specific form element. Please don't feel disappointed about the class code. Were you expecting hundreds of lines? Remember that we're going to implement the power of Inheritance in PHP to create the subclasses for radio buttons, checkboxes, input text elements and even more. Thus, let's take a look at the structure of each derived subclass.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek