Home arrow PHP arrow Page 3 - PHP 5 and Polymorphism

Applying Polymorphic Design - PHP

This article explains what polymorphism is and how it applies to object oriented design in particular. It also explains the pros and cons of polymorphism when working with certain versions of PHP.

TABLE OF CONTENTS:
  1. PHP 5 and Polymorphism
  2. What is Polymorphism?
  3. Applying Polymorphic Design
  4. Late Binding in PHP 5, or, the Lack Thereof
By: David Fells
Rating: starstarstarstarstar / 49
March 06, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: