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 I
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    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:
      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 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


       · Eve the subject has been treated many times on books and articles, the article is...
       · That's a really awkward place to stop Part I. If you didn't have time to finish the...
       · Uh, please pardon the tone of my post above. You made it quite clear in the summary...
       · Your apologies are accepted. No problem about that. Even my schedule is pretty...
       · In trying to understand all that was said I am a little confused about some things...
       · Well I have some expirience programing php and I would like to suggest that in many...
       · Sorry, I forgot to include a sample of the “alternative syntax” I was referring to....
       · Yes, you're correct. Sometimes when you have many parameters to be passed to the...
       · Hi friend,I guess you're refering to the ternary operator. Well, it's an easy way...
       · Hi there,I guess you're talking about the ternary operator. It's often used to...
       · The usage of the :: syntax is the following. You can call a function defined in an...
       · Thanx for the great tutorial, I've used in some way OO programing in my scripts but...
       · Hi dear friend,I'm glad you found this tutorial useful for your growing...
       · Is there any reason why u decided to use PHP4 and its object syntax? Wouldn't it be...
       · Despite the fact that PHP5 offers much better support for OOP, their usage is still...
       · Thanks for sharing us your very nice article! jen
       · Thank you for your compliments about the article. Your comments are deeply...
     

       

    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