Polymorphism in PHP


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

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements