Polymorphism in PHP (Explained with Examples)
Polymorphism is one of the four fundamental principles of Object-Oriented Programming (OOP) in PHP. The term “Polymorphism” is derived from two Greek words:
- Poly → Many
- Morphism → Forms
Polymorphism in PHP is the ability of an object to take on multiple forms of behavior. It allows the same method call to perform different behaviors depending on the object’s class at runtime.
In other words, polymorphism allows objects of different classes to be treated through a common interface or parent class, while each class provides its own implementation. In simple words:
- One interface, many implementations
- Same method name, different behavior
With the help of polymorphism, PHP applications become flexible, scalable, maintainable, and loosely coupled. This OOP feature enables developers to write extensible code, where new functionality can be added without modifying existing code. Therefore, polymorphism is one of the most important concepts in PHP OOP and is essential for modern PHP applications.
How to Achieve Polymorphism in PHP?
PHP mainly supports runtime polymorphism, which you can achieve it using the following ways:
- Method Overriding
- Abstract Classes
- Interfaces
PHP does not support traditional compile-time polymorphism, such as method overloading found in languages like Java. However, you can achieve similar behavior using default arguments, variable-length arguments, or magic methods (such as __call()).
Polymorphism Using Method Overriding in PHP
Method overriding is one of the most common ways to achieve runtime polymorphism in PHP. It occurs when a child class (subclass) provides its own specific implementation of a method that is already defined in its parent class (superclass).
At runtime, PHP decides which method to execute based on the object’s class, not the reference type. Polymorphism using method overriding allows the same method call to execute different behaviors or operations, depending on the object’s class at runtime. Let’s take a simple example based on achieving polymorphism using method overriding in PHP.
Example 1: Polymorphism Using Method Overriding
<?php
class Animal {
public function sound() {
echo "Animal makes a sound \n";
}
}
class Dog extends Animal {
public function sound() {
echo "Dog barks \n";
}
}
class Cat extends Animal {
public function sound() {
echo "Cat meows \n";
}
}
$animal1 = new Dog();
$animal2 = new Cat();
$animal1->sound();
$animal2->sound();
?>Output:
Dog barks Cat meows
In this example:
- We have created a parent class named Animal, which contains a method called sound(). This method prints the message “Animal makes a sound.”
- Then, we have created two child classes, Dog and Cat, which extend the parent class Animal.
- Both child classes override the sound() method, providing their own specific implementations.
- As a result, the same method name (sound()) produces different behavior depending on the object’s class at runtime.
- The sound() method of the Dog class prints the message “Dog barks”.
The sound() method of the Cat class prints the message “Cat meows”. - PHP determines which method to execute at runtime, not at compile time, based on the object’s class.
- This behavior is known as runtime polymorphism in PHP.
Example 2: Polymorphism Using Parent Class Reference
<?php
class Animal {
public function sound() {
echo "Animal makes a sound";
}
}
class Dog extends Animal {
public function sound() {
echo "Dog barks \n";
}
}
class Cat extends Animal {
public function sound() {
echo "Cat meows \n";
}
}
function makeSound(Animal $animal) {
$animal->sound();
}
$dog = new Dog();
$cat = new Cat();
// Passing an object of different class as an argument.
makeSound($dog);
makeSound($cat);
?>Output:
Dog barks Cat meows
In this example:
- Outside the classes, we have defined a function named makeSound(), which accepts a parameter of the parent class type Animal.
- Inside the function, we call the sound() method using the reference variable $animal.
- We then pass different child class objects to the same makeSound() function.
- PHP automatically calls the correct overridden method based on the object’s class at runtime.
- This approach results in loose coupling, where the function depends only on the parent type, not on specific child classes.
- This behavior demonstrates true runtime polymorphism in PHP.
Passing child objects to a function that accepts a parent type is one of the best examples of polymorphism and loose coupling in PHP.
To understand method overriding in more detail with real-world examples, visit the Method Overriding in PHP chapter.
Polymorphism Using Abstract Classes in PHP
Another way to achieve polymorphism in PHP is by using abstract classes. An abstract class allows us to define concrete methods as well as abstract methods that child classes must implement with their own implementations.
We can use abstract classes when we want to share common logic or common behavior and enforce method implementation for different child classes. At runtime, PHP decides which implementation to execute based on the object’s class. Let us take a simple example to understand this.
Example 3: Polymorphism Using Abstract Classes
<?php
abstract class Shape {
abstract public function calculateArea();
}
class Rectangle extends Shape {
// Implementation for calculating area of rectangle.
public function calculateArea() {
return 10 * 5;
}
}
class Circle extends Shape {
// Implementation for calculating area of circle.
public function calculateArea() {
return 3.14 * 5 * 5;
}
}
$shapes = [new Rectangle(), new Circle()];
foreach ($shapes as $shape) {
echo $shape->calculateArea() . "\n";
}
?>Output:
50 78.5
This example shows you to work with different shape objects through a common interface. In this example:
- We have created an abstract class named Shape, which contains an abstract method calculateArea(). This method has no implementation in the abstract class.
- Then, we have created two child classes, Rectangle and Circle, that extend the abstract class Shape.
- Each child class provides a different implementation of the calculateArea() method.
- After that, we created a variable named $shapes and assigned it an array of child class objects.
- We cannot create an object of the abstract class Shape.
- Using a loop, we call the same method calculateArea() using the parent class reference.
- PHP automatically executes the correct method implementation at runtime, based on the object’s class.
- This behavior demonstrates runtime polymorphism using abstract classes.
Example 4: Payment Gateway System (Advanced Real-World Example)
<?php
abstract class Payment {
abstract public function pay($amount);
}
class PayPal extends Payment {
public function pay($amount) {
return "Paid $amount using PayPal \n";
}
}
class Stripe extends Payment {
public function pay($amount) {
return "Paid $amount using Stripe \n";
}
}
function processPayment(Payment $payment, $amount) {
echo $payment->pay($amount);
}
processPayment(new PayPal(), "$500");
processPayment(new Stripe(), "$1000");
?>Output:
Paid $500 using PayPal Paid $1000 using Stripe
In this example:
- We have defined an abstract class named Payment with an abstract method pay().
- Then, we have created two child classes, PayPal and Stripe, that extend the abstract class Payment.
- We have implemented the pay() method differently in each child class.
- Outside the classes, we have defined a function named processPayment().
- We call the processPayment() function by passing different child class objects and the amount as arguments.
- PHP automatically calls the appropriate overridden method at runtime, based on the object’s class.
- This behavior demonstrates runtime polymorphism using abstract classes.
- In future if necessary, we can add new payment methods without modifying the existing code.
Polymorphism Using Interfaces in PHP
This is the third way to achieve polymorphism in PHP. An interface in PHP defines a contract that classes can implement, ensuring that they provide their own specific implementations of declared methods. An interface can contain only declarations, not method implementations.
We use an interface when we want to define common behavior without implementation. Different classes can implement the same interface, allowing them to be treated polymorphically. At runtime, PHP decides which implementation to execute based on the object’s class. Let us take a simple example to understand this.
Example 5: Polymorphism Using Interface
<?php
interface Vehicle {
public function speed();
}
class Car implements Vehicle {
public function speed(){
return "Car speed is 120 km/h \n";
}
}
class Bike implements Vehicle {
public function speed(){
return "Bike speed is 80 km/h \n";
}
}
function vehicle(Vehicle $vehicle){
echo $vehicle->speed();
}
$car = new Car();
$bike = new Bike();
vehicle($car);
vehicle($bike);
?>Output:
Car speed is 120 km/h Bike speed is 80 km/h
In this example:
- We have defined an interface named Vehicle, which contains a method speed().
- Then, we have created two classes, Car and Bike, which implement the same interface.
- Each class provides its own specific implementation of the speed() method.
- Outside the interface and classes, we have defined a function named vehicle() with a parameter $vehicle of the interface type Vehicle.
- Inside the vehicle() function, we call the speed() method using the variable $vehicle.
- We create objects of the child classes Car and Bike and pass different objects to the same vehicle() function.
- PHP automatically calls the appropriate method implementation at runtime, based on the object’s class.
- This demonstrates runtime polymorphism using interfaces in PHP.
Advantages of Polymorphism in PHP
There are the following advantages of polymorphism in PHP:
1. Code Reusability
- Polymorphism allows us to use the same method name for different operations. This increases code reusability and helps reduce duplicate code.
2. Flexibility and Extensibility
- Polymorphism provides flexibility and extensibility, allowing us to add new classes based on requirements without modifying existing code. This supports the Open/Closed Principle.
3. Improved Maintainability
- It makes the code easier to debug, update, and maintain. Changes made in one class do not affect other classes, which improves long-term maintainability.
4. Better Code Readability
- Polymorphism improves code readability, making the code easier to understand and manage, especially in large PHP applications.
5. Runtime Dynamic Method Binding
- Polymorphism supports runtime dynamic method binding, which means PHP decides which method implementation to execute at runtime, based on the object’s class.
6. Loose Coupling
- Polymorphism promotes loose coupling by allowing code to depend on interfaces or parent classes rather than concrete implementations.
Conclusion
Polymorphism is a powerful OOP feature in PHP that simplifies code and allows us to write code with greater flexible, extensible, and maintainable applications. It promotes code reusability and makes our code more adaptable to future changes and additions.
By using method overriding, interfaces, and abstract classes, we can effectively achieve polymorphism in PHP. We hope that you have clearly understood the basic concept of polymorphism and practiced the examples provided in this tutorial.

