Home arrow PHP arrow Page 4 - Inheritance and Polymorphism in PHP: Building a Form Generator - Part I

Applying Inheritance: Creating Subclasses - PHP

Object Oriented Programming (OOP) is a powerful technology for designing Web applications. This article is the first in a three part series that will explain two of the bases of OOP, Inheritance and Polymorphism, and apply these principles to building an extensible form generator.

TABLE OF CONTENTS:
  1. Inheritance and Polymorphism in PHP: Building a Form Generator - Part I
  2. A Quick Look at Basic OOP Concepts
  3. The Base Class of the Form Generator
  4. Applying Inheritance: Creating Subclasses
By: Alejandro Gervasio
Rating: starstarstarstarstar / 50
April 05, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Having defined the generic base class for the form generator, it's fairly easy to derive any number of subclasses tied to each particular form element. After all, that's the big advantage of Inheritance. From this point onward, every defined subclass will inherit the $label, $name and $style properties, previously defined within the super class, as well as the "generateHTML()" method. Let's make Inheritance work for us and define the subclasses for building the form generator. Here are the initial definitions for the each form element's class:

// class definition for input text object

class inputTextObject extends formObject {

var $value;

var $maxlength;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for hidden field object

class hiddenFieldObject extends formObject {

var $value;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for radio button object

class radioButtonObject extends formObject {

var $value;

var $checked;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for checkbox object

class checkBoxObject extends formObject {

var $value;

var $checked;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for select box object

class selectObject extends formObject {

var $size;

var $options;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for textarea object

class textAreaObject extends formObject {

var $value;

var $wrap;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for button object

class buttonObject extends formObject {

var $value;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for password object

class passwordObject extends formObject {

var $maxlength;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for file object

class fileObject extends formObject {

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for image object

class imageObject extends formObject {

var $value;

var $src;

var $alt;

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

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

// properties setup

}

function generateHTML(){

}

}

// class definition for submit object

class submitObject extends formObject {

var $value;

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

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

// properties setup

}

function generateHTML(){

}

}

Phew, I think that's the entire list for subclass definitions. In the above listing, we've implemented Inheritance by creating the proper subclasses that extend the original "formObject" base class, in order to generate the different form elements. Now, our form generator is beginning to be progressively modeled. While the subclasses have been defined with empty "generateHTML()" methods, as you can guess, the final version will include the logic for generating the HTML code for every specific element. For now we'll keep it pretty skeletal, which is handy for keeping the code easily readable.

Summary

And that's it for now. In this first part, we've been glancing at one of the big pillars of OOP in PHP, Inheritance. We've been using its capabilities for building an extensible form generator. Starting from a generic "formObject" base class, we've extended its definition to derive the needed subclasses.

Hopefully this has been a practical approach to demonstrate the use of inherited methods and properties and their correct syntax. However, the best part is coming up. In the second part of this article series, we'll be exposing the complete source code for each subclass and explaining another core concept of OOP: Polymorphism. In the meantime, have some fun classing with PHP!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: