Polymorphism in PHP
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. In PHP, polymorphism is primarily achieved through:
1. Method Overriding (Runtime Polymorphism)
This occurs when a child class provides a specific implementation of a method already defined in its parent class.
class Animal { public function makeSound() { return "Some generic animal sound"; } } class Dog extends Animal { public function makeSound() { return "Bark!"; } } class Cat extends Animal { public function makeSound() { return "Meow!"; } } $animals = [ new Animal(), new Dog(), new Cat() ]; foreach ($animals as $animal) { echo $animal->makeSound() . "\n"; } // Output: // Some generic animal sound // Bark! // Meow!
2. Interface Polymorphism (Compile-time Polymorphism)
PHP interfaces define a contract that implementing classes must follow, enabling polymorphism.
interface Shape { public function calculateArea(); } class Circle implements Shape { private $radius; public function __construct($radius) { $this->radius = $radius; } public function calculateArea() { return pi() * pow($this->radius, 2); } } class Rectangle implements Shape { private $width; private $height; public function __construct($width, $height) { $this->width = $width; $this->height = $height; } public function calculateArea() { return $this->width * $this->height; } } $shapes = [ new Circle(5), new Rectangle(4, 6) ]; foreach ($shapes as $shape) { echo "Area: " . $shape->calculateArea() . "\n"; }
3. Abstract Classes
Abstract classes can also be used to implement polymorphism by defining abstract methods that must be implemented by child classes.
abstract class PaymentMethod { abstract public function processPayment($amount); } class CreditCardPayment extends PaymentMethod { public function processPayment($amount) { return "Processing credit card payment for $amount"; } } class PayPalPayment extends PaymentMethod { public function processPayment($amount) { return "Processing PayPal payment for $amount"; } } $paymentMethods = [ new CreditCardPayment(), new PayPalPayment() ]; foreach ($paymentMethods as $method) { echo $method->processPayment(100) . "\n"; }
4. Type Hinting with Polymorphism
PHP allows type hinting with interfaces and parent classes to enforce polymorphism:
function processShapes(Shape $shape) { echo "Area: " . $shape->calculateArea() . "\n"; } processShapes(new Circle(3)); processShapes(new Rectangle(2, 4));
Benefits of Polymorphism in PHP
Code Reusability: Write code that works with general types rather than specific implementations
Extensibility: Easily add new classes without modifying existing code
Maintainability: Reduces complex conditional statements
Flexibility: Objects can be treated interchangeably based on their common interface
PHP's implementation of polymorphism follows the same principles as other OOP languages but with its own syntax and features like interfaces, abstract classes, and method overriding.