Share on Google+Share on Google+

PHP Polymorphism Function

PHP Polymorphism Function is one of the feature of OOP. Generally we get polymorphism in two ways: Compile time, Run time. In this tutorial we will study about which polymorphism is supported by PHP and which one is not.

PHP Polymorphism Function:

The PHP Polymorphism Method is one of the feature of OOP language. Generally we get polymorphism in two ways:

  • Compile time
  • Run time

Compile time polymorphism PHP is like function overloading, operator overloading etc. In Polymorphism function overloading we can 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. Depending on the actual number and/or the data type the compiler resolve the actual call .

In operator overloading predefined operators treat as functions and one object calls this function and passes another object as argument. PHP neither supports operator overloading nor function overloading.

Inheritance and virtual functions are the examples of PHP Run time polymorphism. PHP supports Inheritance as well as virtual function as function overriding.

Following examples will exemplify each of the types of polymorphism:

PHP Polymorphism Function Example:


class A{

function Disp(){

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

class B extends A{

function Disp(){

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

class C extends A{


$obj=new B();


$obj2=new C();





Inside the Chlid class
Inside the Base class



Posted on: February 22, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null