PHP
  Home arrow PHP arrow Page 2 - 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 - A Quick Look at Basic OOP Concepts


    (Page 2 of 4 )

     

    Before we go more deeply into explaining the concept of Inheritance, for those new to OOP, let's define the concept of object itself. In the context of OO software, an object can be almost any item or concept, either a physical object (i.e. customers, users, and so forth), or conceptual objects that only exist within software, such as a text input field or a radio button.

    Object-oriented software is designed and built as a set of self-contained objects, with both attributes and operations (functions) that interact to make applications work properly. In OOP parlance, attributes are Properties or variables related to the object. Operations or functions are Methods that belong to the object to perform actions for modifying itself or for some other external effect.

    One of the major pillars of OOP is Encapsulation (also known as data hiding). In simple terms, access to the data within an object must be available via the object's methods, or the interface of the object. Modifiers and accessor methods should provide a convenient way to access and modify objects properties.

    Last, but not least, we need to explain Inheritance. We can create a hierarchical relationship between classes and derived subclasses. A subclass inherits properties and methods from its super class, a fact  we can use to save ourselves some work. If we write a super class that owns properties and methods, and then create subclasses that use the capabilities of the super class, we write it only once, and this is definitely better than writing those methods and properties on each subclass (many times, in other words).

    If a sentence about two classes makes sense with "is a" between the classes, from bottom to top, inheritance can probably be implemented appropriately. Consider that if we have an input text element, it might be thought of as "is a form element." So, if we would have a form element class, then the input text element can inherit from the form element.

    For clarifying the concept of Inheritance, let's show a simple and quick example, since the topic has been thoroughly covered in many books and articles. First, let's define a base class "basicMessage" for, not surprisingly, displaying messages:

    class basicMessage {

    var $message;

    function basicMessage($message){

    $this->message=$message;

    }

    function displayMessage(){

    echo 'Displaying '.$this->message.' from base class<br />'."\n";

    }

    }

     

    Now, let's instantiate an object from the base class:

     

    $msg=&new basicMessage('Hello, I am the Mom');

    $msg->displayMessage();

     

    The output for the above code is the following:

    Displaying Hello, I am the Mom from base class

    That's very simple and straightforward, right? Now, in order to implement Inheritance, let's define a subclass "boldedMessage," which inherits the properties and methods from "basicMessage:"

    class boldedMessage extends basicMessage {

    function boldedMessage($message){

    parent::basicMessage($message);

    }

    }

     

    Please notice that inside the "boldedMessage" constructor, we've called the constructor for "basicMessage," with the following syntax:

    parent::basicMessage($message);

    Now, if we instantiate an object from the subclass "boldedMessage," in the following manner:

    $childmsg=&new boldedMessage('Hello, I am the child');

    $childmsg->displayMessage();

    The visual output is as follows:

    Displaying Hello, I am the child from base class

    Certainly, the subclass is happily inheriting the $message property and the "displayMessage()" method from the base class. Also, for proper implementation, the base class definition must be available, for deriving any number of subclasses. Please don't forget that!

    If we ever change our mind, and decide to add the own "displayMessage()" method to the subclass "boldedMessage," overriding the original base class method, the class definition would look like this:

    class boldedMessage extends basicMessage {

    function boldedMessage($message){

    parent::basicMessage($message);

    }

    //override base class displayMessage() method

    function displayMessage(){

    echo '<strong>Displaying '.$this->message.' from subclass</strong><br />'."\n";

    }

    }

    By this point, the subclass is overriding the "displayMessage()," exposing its own method definition, in this case displaying a bolded message. Just by instantiating a new object from the redefined class, in a similar way to the previous examples:

    $childmsg=&new boldedMessage('Hello, I am the child');

    $childmsg->displayMessage();

    the output would be a bolded string message:

    Displaying Hello, I am the child from subclass 

    As you can see, any number of derived subclasses from the base class will inherit its properties and methods. Indeed, this is extremely useful in OOP. Just think about it. If we have a super class containing the core properties and methods, and for instance, we need to add another method to it, the new method will be automatically inherited from all of the defined subclasses. If you think that's not enough power, well you should run for president!

    Now let's take a more practical approach and use OOP in PHP to build something useful for Web applications. Did you read this article's title? Okay, we're going to develop an extensible form generator, using the goodies of Inheritance and Polymorphism. Keep reading.

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