__call() is triggered when invoking inaccessible methods in an object context.
__callStatic() is triggered when invoking inaccessible methods in a static context.
The $name argument is the name of the method being called. The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.
More :http://php.net/manual/en/language.oop5.overloading.php
<?php
class math {
function __call($name, $arg) {
switch ($name) {
case 'add':
if(count($arg)==2) return $arg[0] + $arg[1];
else if (count($arg)==3) return $arg[0] + $arg[1] + $arg[2];
else if (count($arg)==4) return $arg[0] + $arg[1] + $arg[2]+ $arg[3];
else if (count($arg)==5) return $arg[0] + $arg[1]+ $arg[2]+ $arg[3]+ $arg[4];
else if (count($arg)==6) return $arg[0] + $arg[1]+ $arg[2]+ $arg[3]+ $arg[4]+ $arg[5] ;
else return "Too longgg..."; break;
case 'sub':
return $arg[0] - $arg[1]; break;
case 'div':
return $arg[0] / $arg[1]; break;
}
}
}
$calculator = new math;
echo $calculator->add(2,3); echo "<br>"; #5
echo $calculator->add(2,3,4); echo "<br>"; #9
echo $calculator->add(2,3,4,5); echo "<br>"; #14
echo $calculator->add(2,3,4,5,6); echo "<br>"; #20
echo $calculator->add(2,3,4,5,6,7); echo "<br>"; #27
?>
Return to Kesavan Muthuvel's home page. Please send comments on these web pages to hi@kesavan.info (or) Feedback here CopyLeft (Ͻ) 2008 - 2023 Kesavan Muthuvel More on CopyLeft |