Method Overloading in PHP


In this tutorial, we will learn what is Method Overloading in PHP. Method Overloading is a feature in object-oriented PHP in which we can create a method serval times with the same name but with different paraments. At the time of the call, the method will be called according to input parameters. At this time it might be sound confusing but the following example will help you a lot to understand the concept of Method Overloading.

Example Program:

<?php

class Shape {
const PI = 3.142 ;

function __call($name,$arg){
	if($name == 'area')
		switch(count($arg)){
			case 0 : return 0 ;
			case 1 : return self::PI * $arg[0] ;
			case 2 : return $arg[0] * $arg[1];
		}
	}
}
$circle = new Shape();
echo $circle->area(5);


$rect = new Shape();
echo $rect->area(5,10);

?>

 



Spread the love:

Please share this page on social media with your friends..

Facebook share Twitter Linkedin share