PHP
  Home arrow PHP arrow Page 3 - PHP 5 and Polymorphism
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? 
PHP

PHP 5 and Polymorphism
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 27
    2006-03-06


    Table of Contents:
  • PHP 5 and Polymorphism
  • What is Polymorphism?
  • Applying Polymorphic Design
  • Late Binding in PHP 5, or, the Lack Thereof

  • 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


    PHP 5 and Polymorphism - Applying Polymorphic Design
    ( Page 3 of 4 )

    Continuing with our Person base class example, let’s take a look at a non-polymorphic implementation. The following example shows a really poor way to create an application that uses different types of Person objects. Note that the actual Person classes are omitted; we’re only concerned with the calling code for now.

    <?php
    $name = $_SESSION['name'];
    $myPerson = Person::GetPerson($name);

    switch (get_class($myPerson))
    {
                case 'David'     :
                            $myPerson->AddFeedback('Great Article!',
    'Some Reader', date('Y-m-d'));
                            break;
                case 'Charles'   :
                            $myPerson->feedback[] = array('Some
    Reader', 'Great Editing!');
                            break;
                case 'Alejandro' :
                            $myPerson->Feedback->Append('Awesome
    Javascript!');
                            break;
                default          :
                            $myPerson->AddFeedback('Yay!');
    }
    ?>

    This example shows objects that behave differently and a switch statement to differentiate between the different classes of Person, performing the correct operation on each. Note that the feedback comment in each condition is different. That probably would not be the case in a real application; it’s simply done to elucidate the differences in the class implementations.

    The next example uses polymorphism.

    <?php
    $name = $_SESSION['name'];
    $myPerson = Person::GetPerson($name);
    $myPerson->AddFeedback('Great Article!', 'SomeReader', date('Y-m-
    d'));
    ?>

    Note the lack of the switch statement and, of greater importance, the lack of concern regarding what type of object Person::GetPerson() returned. Person::AddFeedback() is a polymorphic method. The behavior is one hundred percent encapsulated by the concrete class. Remember, whether we’re working with David, Charles or Alejandro, calling code never has to know the concrete class to function, only the base class.

    While I’m sure there are better examples than mine, I think it demonstrates the basic use of polymorphism from the perspective of calling code. We now need to take into consideration the internals of the classes. One of the greatest aspects of inheriting from a base class is that the inheriting class is able to access the behaviors of the parent class, which often serve as nothing more than defaults, but can also be chained to inheriting methods to create more sophisticated behaviors. Below is a simple demonstration of this.

    <?php
    class Person
    {
                function AddFeedback($comment, $sender, $date)
                {
                            // Add feedback to database
                }
    }

    class David extends Person
    {
                function AddFeedback($comment, $sender)
                {
                            parent::AddFeedback($comment, $sender,
    date('Y-m-d'));
                }
    }
    ?>

    Here we have chained the David::AddFeedback() method to the Person::AddFeedback method. You may note that it resembles overloaded methods in C++, Java, or C#. Remember that this is a simplified example, and that the actual code you write will of course be completely dependent on your project.



     
     
    >>> More PHP Articles          >>> More By David Fells
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT