Polymorphism with PHP 5

A php practical real life sample with polymorphism, inheritance and encapsulation in Object Oriented Programming

Polymorphism comes from the two greeks words: poly (many) and morph (form), meaning basically many forms. In Object Oriented Programming we can say that one variable can take the shape we want.

Let's see this with a more real life example: I want to know how much money I've got in my account after earning my royalties and then paying taxes. The two subclasses Income and Bill inherit from the base class Amount. The protected properties amount and description acces is encapsulated and are only accesible by the child classes.

/**
* Account.php
* A real life sample with polymorphism
*
@author freedelta http://freedelta.free.fr
*/
// Base class

class Account
{
    protected $amount;
    protected $description;

    public function __construct($amount, $description)
    {
        $this->amount = $amount;
        $this->description = $description;
    }

    public function getAmount()
    {
      return $this->amount;
    }

    public function getDescription()
    {
      return $this->description;
    }
 }

// Child class #1: money I make
class Income extends Account
{

}

 // Child class #2: Things I should pay
class Bill extends Account
{
    public function __construct($amount, $description)
    {
        $this->amount = -$amount;
        $this->description = $description;
    }
 }

$account[] = new Income(10000000, "Royalties");
$account[] = new Bill(1000, "Taxes");

$format = "%s ... %8.2f\n";
$total = 0;  

foreach ($account as $line) {
printf($format, $line->getDescription(), $line->getAmount());
     $total += $line->getAmount();
}
 
printf($format, "TOTAL ...",$total);

/*
Will display:

Royalties ... 10000000.00
Taxes     ... -1000.00
TOTAL ... ... 9999000.00
*/

Pretty good income, eh? :)

In this other example with geometric shapes we use an abstract class because there is one common method implemented (showWhat). Shape types are diferrent but methods described in the abstract base class are common.

/**
 * polymorph.php - A simple polymorphism code sample
 * @author freedelta http://freedelta.free.fr
 */

// Base class

abstract class Shape
{
    protected function showWhat($msg)
{
        echo "I am a:". $msg ." ";
    }
      // Abstract method to be implemented       abstract function draw();  }
// Child class #1
class Circle extends Shape
{
     function __construct()
{
        parent::showWhat("Circle");
     }
       function draw()
{
        echo "Drawing a circle";      }
 }

 // Child class #2
class Rectangle extends Shape
{
     function __construct()
{  
         parent::showWhat("Rectangle");    
     }
      function draw()
{
        echo "Drawing a rectangle";
      }
 }
// Child class #3
class Triangle extends Shape
{
     function __construct()
{  
         parent::showWhat("Triangle");    
     }
 
     function draw()
{
        echo "Drawing a triangle";
     }
 }
    // Set up an array with some shape instances    $arrShapes = array(new Circle(), new Rectangle(), new Triangle());
   // Iterate through the shapes    for($i = 0; $i < count($arrShapes); $i++)
{
      $arrShapes[$i]->draw();      
   }