Polymorphism in PHP

Google Advertisements

Prev Tutorial Next Tutorial

Polymorphism in PHP

Polymorphism represent more than one form, it is most, it achieve using method overloading and method overriding. This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.

Syntax

<?php

class BaseClass 
{ 
  public function myMethod() 
 { 
 echo "BaseClass method called";
 }
}  
 class DerivedClass extends BaseClass 
 {
  public function myMethod() 
 {
  echo "DerivedClass method called"; 
 }
} 
 function processClass(BaseClass $c) 
 {
  $c->myMethod(); 
 } 
 $c = new DerivedClass(); 
 processClass($c);

?>

Output

DerivedClass method called

Prev Tutorial Next Tutorial

Download Projects

Adsense Advertisements
Free Classified Site – Bedpage

Yahoo Ads Advertisements