Fri May 22 2020

Method Overloading

PHP Scripting1484 views

File Name: method-overloading.php

<?php
	class areaCal {
		
		/* '__call()' function to received method call with parameter */
		function __call($name, $parameter) {
			$ans = 0;
			/* Check call method name */
			if($name == "area") {
				
				/* Count no of parameter */
				$count = count($parameter);
				switch($count) {
					case 1:
						$ans = (22/7)*$parameter[0]*$parameter[0];
						break;
					case 2:
						$ans = $parameter[0]*$parameter[1];
						break;
					default:
						echo "Invalid parameter";
				}
			}
			return $ans;
		}
	}
	
	/* Object of the class 'areaCal' */
	$area = new areaCal();
	echo "Area of Circle: ".number_format((float)$area->area(5), 2, '.', '');
	echo "<br />Area of Rectangle: ".$area->area(5,4);
?>


/* Output */
Area of Circle: 78.57
Area of Rectangle: 20

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.