Polymorphism

  • Polymorphism is the process of creating several objects from specific base classes.

  • A seperate object can have seperate properties and methods which work separately to other objects.

  • However a set of objects could be derived from a parent object and keep some properties of the parent class.

  • There is two ways to achieve in polymorphism :

    1. Compile time(Overloading)

    2. Run time(Overriding)


Overloading

  • If you create more than one function with same name but with different signature.

  • That means it could have different number of parameters, different datatype of the parameter etc. that is known as Compile Time Polymorphism.

  • PHP does not support Overloading.

Overriding

  • If you override any method (either declared as protected or public) declared in superclass and perform anything as you wish in subclass that is known as Run Time Polymorphism.
Example :
<?php

class A{

function display(){

echo "Inside the Base class <br/>";}}

class B extends A{

function display(){

echo "Inside the Chlid class <br/>";}}

class C extends A{

}

$obj=new B();

$obj->display();

$obj2=new C();

$obj2->display();

?<
Output :
Inside the Chlid class
Inside the Base class