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.

  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



Continuing with our Person base class example, lets 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; were only concerned with the calling code for now.

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

switch (get_class($myPerson))
            case 'David'     :
                        $myPerson->AddFeedback('Great Article!',
'Some Reader', date('Y-m-d'));
            case 'Charles'   :
                        $myPerson->feedback[] = array('Some
Reader', 'Great Editing!');
            case 'Alejandro' :
            default          :

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; its simply done to elucidate the differences in the class implementations.

The next example uses polymorphism.

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

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 were working with David, Charles or Alejandro, calling code never has to know the concrete class to function, only the base class.

While Im 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.

class Person
            function AddFeedback($comment, $sender, $date)
                        // Add feedback to database

class David extends Person
            function AddFeedback($comment, $sender)
                        parent::AddFeedback($comment, $sender,

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
escort Bursa Bursa escort Antalya eskort


- 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: