PHP Polymorphism

PHP Polymorphism

There are two types of Polymorphism; i.e. 

  1. Compile time (function overloading)
  2. 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.

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*