jump to navigation

Object Oriented Programming and Polymorphism in PHP May 21, 2012

Posted by Tournas Dimitrios in PHP.

The word polymorphism comes from Greek and means “many forms”. In object-oriented programming, polymorphism is a mechanism by which objects of different types can process data through a single interface . PHP is a loosely typed language , and compared with other OOP programming languages , has it’s limitations . Function overloading , multiclass-inheritance , returning typed-objects are only few of these limitations that comes right now into my mind . Even polymorphism has it’s own implementation  in PHP , let’s focus on this last term .

Let’s pretend we had the duty to evaluate different vehicles , some characteristics are common between them  , some others don’t . An OOP implementation would create an abstract Class , that embeds all common functionality . Each vehicle class will extend  the abstract class  and inherit all it’s  characteristics . Lastly , each vehicle will add or override these characteristics that are unique to it’s own profile . At the end of this article , a PHP-function will act as an interface to “run” these vehicle-object and return some results .

Wrong approach : A method , simulating function overloading . Because PHP does not support function overloading  , an if-else statement , will  test the type of the actual argument and execute statements that correspond to the argument’s type . This technique may ultimately result in a monolithic function or a function implementation that is too knowledgeable of multiple class hierarchies .Sooner or later our code will break , or at least a developer in future updates will face many problems . Could you imagine what could happen if we had to add some more type of vehicle objects , simple , we had to re-factor our interface .

Correct approach : We know that the name of the class defines the type of object that is instantiated from it . So , a “Truck” class produces objects of type “Truck” , a “Motorbike” class produces objects of type “Motorbike” etc …..
In PHP , a class can only extend ONE class . But can implement many Interfaces . Interfaces allow you to create code which specifies which methods a class must implement , without having to define how these methods are handled . Interfaces , can’t be instantiated , only implemented into other classes  . Interestingly , each PHP class that implements an interface , belong to the type of that interface . For example , a class “Truck” implements two interfaces (Vehicle , Racing ) , each object that will be instantiated from this class  is of type of  Truck  , Vehicle , Racing .

Our code has the following structure :

  • An abstract class , “BaseVehicle”  defines common functionality for all classes that inherit it’s structure .
  • An Interface , “IVehicle” , defines a template for some methods . Each class that implements this interface must define the implementation of these functions .
  • Each class that implement this interface (“IVehicle” ) will also belong to this object – type .
  • Creating three Vehicle classes : Motorbike , Car and Truck . Each of these classes belong to a type-object of BaseVehicle , IVehicle and it’s own name .
  • Each Vehicle class will override or add it’s own characteristics into it’s parent class .
  • An interface (function “benchmarking”) is responsible to “run” the implementation of the objects that where derived from the Vehicle classes .

The code :

The Iverhicle interface Class
interface IVehicle{
function getType();
function getCapacity();
function getConsumption() ;

The Abstract BaseVehicle Class

abstract class BaseVehicle implements IVehicle{
protected $type  ;
protected $km ;
protected $capacity ;
protected $lpk ; // Litres per Kilometer
 function __construct($type , $km) {
       $this->type = $type  ;
	   $this->km = $km ;
function getType() {
return ($this->type);
function getCapacity(){
return ("$this->capacity ");
function getConsumption() {
return $this->km * $this->lpk ;

The Motorbike Class
class Motorbike extends BaseVehicle{
protected $lpk = 2 ;
protected $capacity = "2 people" ;


The Car Class
class Car extends BaseVehicle{
protected $lpk = 20 ;
protected $capacity = "2 people and no boot space" ;


The Truck Class
class Truck extends BaseVehicle{
protected $lpk = 200 ;
protected $capacity = "3 people and 10 tonnes of cargo space" ;


Let's run the code
$vehicleArray = array(
new Motorbike("Africa twin" , 2 ),
new Car("Mazerati" , 2 ),
new Truck("Volvo FE" , 2)

function benchmarking( IVehicle $vehicleData) {
echo $vehicleData->getType() . " - " . $vehicleData->getCapacity() .
"  and consumes  : " .$vehicleData->getConsumption()."litres ". "


foreach ($vehicleArray as $vehicleData) {
 benchmarking($vehicleData) ;

Africa twin - 2 people and consumes : 4litres
Mazerati - 2 people and no boot space and consumes : 40litres
Volvo FE - 3 people and 10 tonnes of cargo space and consumes : 400litres


Hope this article has shed some light to PHP’s polymorphism  functionality . Happy coding🙂

Read also :


1. lublin - June 2, 2013

certainly like your web site however you have to
test the spelling on quite a few of your posts. Many of them are rife with spelling problems and I in finding it very troublesome
to tell the reality however I will definitely come again again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s