Greetings! In today's lesson, we'll unravel the concept of polymorphism in PHP's Object-Oriented Programming (OOP). Grasping polymorphism enables us to use a single entity (a method or class) to represent different types in various scenarios. Let's proceed.
Polymorphism, a pillar of OOP, allows one object to embody multiple forms. Visualize a button in software; depending on its type (for instance, a submit button or a radio button), the action resulting from pressing it varies. This dynamic encapsulates the spirit of polymorphism!
PHP supports polymorphism mainly through dynamic or run-time polymorphism, leveraging method overriding within abstract classes. Let's observe dynamic polymorphism in action within a simple application involving shapes. The base Shape
class has an area
method, which calculates the area for shapes. This method is uniquely implemented in the subclasses Rectangle
and Circle
.
php1<?php 2 3abstract class Shape { 4 // Abstract method for calculating area 5 abstract public function area(); 6} 7 8class Rectangle extends Shape { 9 private $length; 10 private $width; 11 12 public function __construct($length, $width) { 13 $this->length = $length; 14 $this->width = $width; 15 } 16 17 // Override the abstract method to calculate rectangle area 18 public function area() { 19 return $this->length * $this->width; 20 } 21} 22 23class Circle extends Shape { 24 private $radius; 25 26 public function __construct($radius) { 27 $this->radius = $radius; 28 } 29 30 // Override the abstract method to calculate circle area 31 public function area() { 32 return pi() * $this->radius * $this->radius; 33 } 34} 35 36$rectangle = new Rectangle(2, 3); 37echo $rectangle->area() . "\n"; // Prints: 6 38 39$circle = new Circle(5); 40echo $circle->area() . "\n"; // Prints: 78.539816339745
Here, polymorphism shines as the area()
method takes on multiple forms while using the same Shape
abstract class. It behaves differently depending on whether the object is a Rectangle
or a Circle
.
Polymorphism becomes particularly powerful when working with collections of objects, such as arrays. Let's see how it simplifies operations when dealing with an array of different shapes.
Imagine we want to calculate the total area of a group of shapes, which includes both rectangles and circles. With polymorphism, we can easily iterate over an array of Shape
objects and calculate the area for each, regardless of its specific type.
php1<?php 2 3$shapes = [ 4 new Rectangle(2, 3), 5 new Circle(5), 6 new Rectangle(4, 5) 7]; 8 9$totalArea = 0; 10foreach ($shapes as $shape) { 11 $totalArea += $shape->area(); 12} 13 14echo $totalArea; // Prints: 104.53981633974
In this example, we create an array called $shapes
, which holds different instances of Rectangle
and Circle
. As both Rectangle
and Circle
are subclasses of Shape
, they both implement the area()
method, albeit differently. When iterating over the $shapes
array, each element calls its specific implementation of the area()
method, whether it's for a Rectangle
or a Circle
.
This approach demonstrates the power of polymorphism as it avoids the need for complex conditional logic to determine what type of object we are dealing with. Instead, each object handles its own specific logic internally, making the code cleaner, more maintainable, and extensible. Whenever you add a new shape that extends the Shape
class, it will seamlessly integrate into the existing framework without any additional conditional statements.
Great job! We've now learned about polymorphism, observed its implementation, and discovered its applications in PHP. Understanding how PHP handles polymorphism allows you to create flexible and reusable code. Now, prepare for hands-on practice tasks. Apply what you've learned and excel in PHP programming. Happy coding!