codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

46 / 56 topics
43Object-Oriented Programming in PHP44Classes and Objects45Inheritance in PHP46Polymorphism in PHP47Interfaces in PHP48Traits in PHP
Tutorials/PHP/Polymorphism in PHP
🐘PHP

Polymorphism in PHP

Updated 2026-05-15
10 min read

Polymorphism in PHP

Introduction

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.

Concept

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:

  1. Inheritance: A subclass (child class) inherits properties and methods from a superclass (parent class).
  2. Interfaces: A class can implement one or more interfaces, which define a set of methods that the class must provide.

We will focus on inheritance in this tutorial to demonstrate polymorphism.

Examples

Let's start by defining our base Shape class and then create subclasses for different shapes.

Step 1: Define the Base Class

First, we'll create a Shape class with a method calculateArea() that we expect all subclasses to implement.

php
1class Shape {
2 public function calculateArea() {
3 // This method will be overridden by subclasses
4 return "Area calculation not implemented.";
5 }
6}

Step 2: Create Subclasses

Now, let's create two subclasses: Circle and Rectangle. Each subclass will override the calculateArea() method to provide its own implementation.

php
1class Circle extends Shape {
2 private $radius;
3
4 public function __construct($radius) {
5 $this->radius = $radius;
6 }
7
8 public function calculateArea() {
9 return pi() * pow($this->radius, 2);
10 }
11}
12
13class Rectangle extends Shape {
14 private $width;
15 private $height;
16
17 public function __construct($width, $height) {
18 $this->width = $width;
19 $this->height = $height;
20 }
21
22 public function calculateArea() {
23 return $this->width * $this->height;
24 }
25}

Step 3: Use Polymorphism

We can now create objects of these subclasses and use the calculateArea() method polymorphically.

php
1$circle = new Circle(5);
2$rectangle = new Rectangle(4, 6);
3
4echo "Circle Area: " . $circle->calculateArea() . "
5"; // Outputs: Circle Area: 78.539816339745
6echo "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.

Step 4: Dynamic Method Dispatch

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.

php
1function displayArea(Shape $shape) {
2 echo "The area is: " . $shape->calculateArea() . "
3";
4}
5
6displayArea($circle); // Outputs: The area is: 78.539816339745
7displayArea($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.

What's Next?

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!


PreviousInheritance in PHPNext Interfaces in PHP

Recommended Gear

Inheritance in PHPInterfaces in PHP