It's an interesting class. Incidentally, welcome to the community Sjag! However,
func_get_args wouldn't cut it here because I think Gibou intended it to be so he can call the function for what it is. Instead of:
php Code:
$pObject->maths('add', 5, 10);
You could do:
I've come with a much more condensed function - still using the unsurpassed power of the magic method,
__call! Here it is - based on Gibou's function:
php Code:
class Maths
{ public function __call($szMethod,
$aArgs) { $iTotal =
$aArgs[
0];
unset($aArgs[
0]
);
switch($szMethod) { case('add'):
foreach($aArgs as $iValue) $iTotal +=
$iValue;
break;
case('minus'):
foreach($aArgs as $iValue) $iTotal -=
$iValue;
break;
case('divide'):
foreach($aArgs as $iValue) $iTotal /=
$iValue;
break;
case('multiply'):
foreach($aArgs as $iValue) $iTotal *=
$iValue;
break;
} return $iTotal;
}}
You can then use it like so:
php Code:
$pM =
new Maths
();
var_dump($pM->
add(7,
10,
5,
2));
Which would add 10 to 7, 5 to 17, and 2 to 22, totalling 24. You can also do more complex things such as:
Which multiplies 10 by 5, to give us 50, then 50 by 2 to give us 100! I'm quite proud of this as I've never been any good at maths :( !