How to achieve and use Polymorphism in PHP

Polymorphism – the concept

 

Polymorphism is a Greek word that means “many forms”. In object-oriented programming, polymorphism is a mechanism by which objects of different types can process data through a single interface. Using a typical illustrative example, a program might define a type of object called Animal, containing a function called talk() and, through inheritance, derive object sub-types such as Cat, Dog, etc. that each have their own specific implementation of the talk() function. The mechanism of polymorphism allows such a program to call the talk() function without actually knowing whether it is calling it for a Cat, Dog, or any other derivative Animal, while executing the correct function for the specific type of Animal.

 

Why method polymorphism cannot be achieved in PHP?
In languages like Java where in a function sum (int, int) differs from sum (float, float), the method sum() has many forms depending on the parameters being passed to it.

But in PHP it is not possible because you can have a method that accepts two parameters and call it by passing three parameters (allows variable arguments). This is because PHP is not strict and contains methods like func_num_args() and func_get_arg() to find the number of arguments passed and get a particular parameter.

 

Polymorphism is not the same as method overloading or method overriding (in OOP, a method is a function that belongs to a class, while the class variables are referred to as its members) and should not be confused with these.

 

Method overloading is where a class has two or more functions of the same name, but accepting a different number and/or data types of parameters.

 

Method overriding is where a child class (one derived through inheritance) defines a function with the same name and parameters as a function in its parent class, but provides a different implementation of the function. This is not the same as polymorphism, as it does not necessarily involve late-binding, however overriding is part of the mechanism by which polymorphism is accomplished.

 

 

PHP 5 & Polymorphism
Since PHP 5 introduces the concept of Type Hinting, polymorphism is possible with class methods. The basis of polymorphism is Inheritance and overridden methods.

[php]

<?php

class BaseClass {
public function myMethod() {
echo "BaseClass method called";
}
}

class DerivedClass extends BaseClass {
public function myMethod() {
echo "DerivedClass method called";
}
}

function processClass(BaseClass $c) {
$c->myMethod();
}

$c = new DerivedClass();
processClass($c);

?>

[/php]

 

OUTPUT:

DerivedClass method called

 

In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BaseClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

 

I hope the above makes sense and hasn’t been too confusing as you know this is difficult to explain in clear English 🙁