Polymorphism is a core concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables methods to do different things based on the object it is acting upon, while using a single interface. In PHP, polymorphism can be achieved through inheritance and interfaces.
In this tutorial, we will explore how to use polymorphic behavior in PHP by creating a simple example involving shapes. We'll define a base class Shape and then create subclasses like Circle and Rectangle. Each subclass will implement its own version of a method that calculates the area, demonstrating polymorphism.
Polymorphism allows methods to have different implementations based on the object calling them. This is particularly useful when you want to write code that can work with objects of different classes through a common interface.
In PHP, polymorphism can be implemented using:
We will focus on inheritance in this tutorial to demonstrate polymorphism.
Let's start by defining our base Shape class and then create subclasses for different shapes.
First, we'll create a Shape class with a method calculateArea() that we expect all subclasses to implement.
1class Shape {2public function calculateArea() {3// This method will be overridden by subclasses4return "Area calculation not implemented.";5}6}
Now, let's create two subclasses: Circle and Rectangle. Each subclass will override the calculateArea() method to provide its own implementation.
1class Circle extends Shape {2private $radius;34public function __construct($radius) {5$this->radius = $radius;6}78public function calculateArea() {9return pi() * pow($this->radius, 2);10}11}1213class Rectangle extends Shape {14private $width;15private $height;1617public function __construct($width, $height) {18$this->width = $width;19$this->height = $height;20}2122public function calculateArea() {23return $this->width * $this->height;24}25}
We can now create objects of these subclasses and use the calculateArea() method polymorphically.
1$circle = new Circle(5);2$rectangle = new Rectangle(4, 6);34echo "Circle Area: " . $circle->calculateArea() . "5"; // Outputs: Circle Area: 78.5398163397456echo "Rectangle Area: " . $rectangle->calculateArea() . "7"; // Outputs: Rectangle Area: 24
In this example, the calculateArea() method is called on objects of different classes (Circle and Rectangle). However, each class provides its own implementation of the method, demonstrating polymorphism.
PHP supports dynamic method dispatch, which means that the method to be executed is determined at runtime. This allows us to use a common interface (the base class) to call different methods based on the actual object type.
1function displayArea(Shape $shape) {2echo "The area is: " . $shape->calculateArea() . "3";4}56displayArea($circle); // Outputs: The area is: 78.5398163397457displayArea($rectangle); // Outputs: The area is: 24
In this function, we pass a Shape object to the displayArea() function. Depending on whether the object is an instance of Circle or Rectangle, the appropriate calculateArea() method is called.
After understanding polymorphism through inheritance, you can explore how interfaces in PHP provide another way to achieve polymorphic behavior. Interfaces allow you to define a set of methods that must be implemented by any class that implements the interface, providing a more flexible and decoupled approach to polymorphism.
Stay tuned for more tutorials on advanced OOP concepts in PHP!