What is Polymorphism?
In PHP, Polymorphism Greek word poly and morphism. Poly means "many" and morphism means property. It helps us to assign more than one property.
- Function Overloading (Method Overloading)
- Function Overriding ( Method Overriding)
In PHP, function overloading and overriding are the most important features of oops. we will discuss how to implement function overriding and function overloading.
1. Function Overloading
In PHP, function overloading means the multiple same function names with different signatures (Parameters) and the function performs different tasks according to the number of arguments.
PHP doesn't directly support method overloading, but it can be simulated using the __call() magic method
There are various overloading properties of magic methods and they have different work, so we should also know about some magic methods.
- __set(): triggered while initializing overloaded properties.
- __get(): triggered while using overloaded properties with PHP print statements.
- __isset(): This magic method is invoked when we check overloaded properties with the asset() function
- __unset(): Similarly, this function will be invoked using PHP unset() for overloaded properties.
- __call() – triggered while invoking overloaded methods in the object context.
- __callStatic() – triggered while invoking overloaded methods in a static context.
Example 1
<?php
class Parents{
public function age($parameter1) {
echo "My Age is =".$parameter1;
}
public function age($parameter1, $parameter2) {
echo "My Age is =".$parameter1;
}
}
$object = new Parents;
$object->age(29);
?>
Output
Fatal error: Cannot redeclare Parents::age()
Example 2 ( Magic method )
<?php
class Resolve{
function __call($name, $age){
if($name == 'details')
switch(count($age)){
case 0 : return 0 ;
case 1 : return $age[0] ;
case 2 : return $age[0]." & Age ".$age[1];
}
}
}
$obj1 = new Resolve();
echo 'My Name is :'.$obj1->details("Radha").'</br>';
$obj2 = new Resolve();
echo 'My Name : '.$obj2->details("Radha",18);
?>
Output
My Name is :Radha
My Name : Radha & Age 18
2. Function Overriding
In PHP, function overriding means the same function name with the same signature (parameter). All overloading methods must be defined as Public.
The main purpose of using the method Overriding is to change the behaviours of the parent class method.
Types of Overriding in PHP.
There are two types of Overriding in PHP. We will see examples to understand both “Property Overloading” and “Method Overloading”
- Property Overriding
- Method Overriding
Example 1 (Overriding Properties)
<?php
class base{
public $name="PHP";
}
class derived extends base{
public $name="Rasmus Lerdorf"; // overriding name properties
}
$obj= new derived();
echo $obj->name;
?>
Output
Rasmus Lerdorf
Example2 (Overriding function)
<?php
class base{
public function cal($a,$b){
return $a+$b;
}
}
class derived extends base{
public function cal($a,$b){ // overriding function
return $a*$b;
}
}
$obj= new derived();
echo $obj->cal(3,5);
?>
Output
15
Note: Here cal() function is overriding in derived class
Example 3
<?php
class base{
public function cal($a,$b,$c){
return $a+$b+$c;
}
}
class derived extends base{
public function cal($a,$b,$c){ // Here passing 3 parameter that is wrong
return $a*$b*c;
}
}
$obj= new derived();
echo $obj->cal(3,5); // Here passing 2 parameter
?>
Output
Warning: Missing argument 3 for derived::cal(), called in
Note: Here parameter (signature) should be the same.
Prev