Home Tutorial Php Phpoop PHP Polymorphism Function

 
 

Share on Google+Share on Google+
PHP Polymorphism Function
Posted on: February 22, 2010 at 12:00 AM
Advertisement
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:

<?php

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();

$obj->Disp();

$obj2=new C();

$obj2->Disp();

?>

 

Output:

Inside the Chlid class
Inside the Base class

 

Advertisement

Related Tags for PHP Polymorphism Function:


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.

Posted on: February 22, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com