View Single Post
Old 11-27-2007, 03:50 PM   #4 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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:

php Code:
$pObject->add(5, 10);

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:

php Code:
var_dump($pM->multiply(5, 10, 2));

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 :( !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote