Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Polymorphism In Php
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Object Oriented Tutorials
  3. Polymorphism In Php

PHP Object Oriented Tutorial

Basic Object Oriented Tutorials

  • Object Oriented Programming In PHP
  • Advantage of OOPS In PHP
  • Important OOPS concepts
  • Oops Features In PHP
  • Class In PHP
  • Object In PHP
  • Constructor and Destructor
  • Inheritance in PHP
  • Encapsulation In Php
  • Abstract Classes In PHP
  • Interface In PHP
  • Polymorphism In Php

Polymorphism In Php

Polymorphism is a long word for a very simple concept. Polymorphism describes a pattern in object-oriented programming in which classes have different functionality while sharing a common interface. In the programming world, polymorphism is used to make applications more modular and extensible

The provision of a single interface to entities of different types. Basically it means PHP is able to process objects differently depending on their data type or class. This powerful feature allows you to write interchangeable objects that sharing the same interface.

Example for Polymorphism  

<?php
interface Shape {
	public function name();
}

class Circle implements Shape {
	public function name() {
		echo "I am a circle";
	}
}

class Triangle implements Shape {
	public function name() {
		echo "I am a triangle";
	}
}

function test(Shape $shape) {
	$shape->name();
}

test(new Circle()); // I am a circle
test(new Triangle()); // I am a triangle
?>

Above example, test(Shape $shape) function declares(type hints) its only parameter to be Shape type. This function is not aware of Circle and Triangle classes. When either class is passed to this function as a parameter, it processes respectively.

Example 2

<?php
interface Shape {
   public function getArea();
}
class Square implements Shape {
   private $width;
   private $height;

   public function __construct($width, $height) {
      $this->width = $width;
      $this->height = $height;
   }

   public function getArea(){
      return $this->width * $this->height;
   }
}
class Circle implements Shape {
   private $radius;

   public function __construct($radius) {
      $this->radius = $radius;
   }

   public function getArea(){

      return 3.14 * $this->radius * $this->radius;
   }
}
function calculateArea(Shape $shape) {
   return $shape->getArea();
}

$square = new Square(8, 8);
$circle = new Circle(7);

echo calculateArea($square), "<br/>";
echo calculateArea($circle);
?>
Series: PHP Object Oriented Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
polymorphism in php easytolearning real-time example of polymorphism in php inheritance and polymorphism in php polymorphism in php in hindi interface and polymorphism in php polymorphism in php oops concept php polymorphism function encapsulation in php

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.