1. Introduction
Polymorphism is an important concept in Object-Oriented Programming (OOP). In simple words, polymorphism means one name, many forms.
The word Polymorphism comes from two Greek words:
- Poly → meaning many
- Morph → meaning forms
In PHP, polymorphism allows different classes to use the same method name but perform different actions. This is very useful when multiple classes share a common interface but need different implementations.
- Improves code flexibility
- Makes programs easier to extend
- Reduces code duplication
- Makes code easier to maintain
2. Syntax
In PHP, polymorphism is usually implemented using interfaces or inheritance.
Basic syntax using an interface:
interface InterfaceName {
public function methodName();
}
class ClassOne implements InterfaceName {
public function methodName() {
// implementation
}
}
class ClassTwo implements InterfaceName {
public function methodName() {
// different implementation
}
}
Here:
- The interface defines a common method.
- Multiple classes implement that interface.
- Each class provides its own implementation of the method.
3. Example
In this example, both Apple and Orange classes implement the same interface method taste(), but each class returns a different result.
interface Fruit {
public function taste();
}
class Apple implements Fruit {
public function taste() {
return "Apple taste is very sweet.";
}
}
class Orange implements Fruit {
public function taste() {
return "Orange taste is very sour.";
}
}
$apple = new Apple();
echo $apple->taste();
echo "<br>";
$orange = new Orange();
echo $orange->taste();
Output:
Orange taste is very sour.
Explanation:
Let’s understand how polymorphism works in the fruit example.
First, we created an interface called Fruit. An interface defines a method that must be implemented by any class that uses it.
interface Fruit {
public function taste();
}
Next, we created two classes:
- Apple
- Orange
Both classes implement the Fruit interface using the implements keyword.
class Apple implements Fruit
Inside each class, we defined the taste() method, but the implementation is different.
For example:
Apple class:
return "Apple taste is very sweet.";
Orange class:
return "Orange taste is very sour.";
After that, we created objects of both classes.
$apple = new Apple();
$orange = new Orange();
When we call the taste() method, each object executes its own version of the method.
echo $apple->taste();
echo $orange->taste();
Even though the method name is the same, the behavior is different. This is exactly what polymorphism means.
4. Real-world Example
Let’s understand polymorphism with a real-world payment system example.
In an online shopping website, users can pay using different methods
- Credit Card
- PayPal
- UPI
Even though the payment process is different, all payment methods follow a common pay() method.
interface Payment {
public function pay($amount);
}
class CreditCard implements Payment {
public function pay($amount) {
echo "Paid $" . $amount . " using Credit Card.";
}
}
class UPI implements Payment {
public function pay($amount) {
echo "Paid $" . $amount . " using UPI.";
}
}
$payment1 = new CreditCard();
$payment1->pay(100);
echo "<br>";
$payment2 = new UPI();
$payment2->pay(200);
In this example:
- The Payment interface defines the pay() method.
- Different classes implement the method differently.
- The same method name performs different tasks.
This is a perfect example of polymorphism in real applications.
Output:
Paid $200 using UPI.
8. Conclusion
PHP polymorphism is a powerful feature of Object-Oriented Programming that allows the same method name to perform different tasks depending on the class that implements it.
In real-world applications such as payment systems, notification services, and API integrations, polymorphism plays a key role in designing scalable and clean code architecture.
PHP Polymorphism – Interview Questions
Q 1: What is polymorphism in PHP?
Q 2: How is polymorphism achieved in PHP?
Q 3: What is method overriding?
Q 4: Is polymorphism related to inheritance?
Q 5: Advantage of polymorphism?
PHP Polymorphism – Objective Questions (MCQs)
Q1. Polymorphism means ______.
Q2. Polymorphism can be achieved via ______.
Q3. Which is an example of runtime polymorphism?
Q4. Polymorphism improves ______.
Q5. Compile-time polymorphism in PHP can be simulated using ______..