PHP Polymorphism
There are two types of Polymorphism; i.e.
- Compile time (function overloading)
- Run time (function overriding)
But PHP “does not support” compile time polymorphism, which means function overloading and operator overloading.
<?php
class Course1{
public function showCourse()
{
echo “Course 1”;
}
}
class Course2 extends Course1{
public function showCourse()
{
echo “Course 2”;
}
}
$student = new Course2();
$student->showCourse();
?>
Output
Course 2
This override the base class method, To achieve polymorphism in PHP you can use abstract methods or interfaces.