truslivii.lev
truslivii.lev

Reputation: 701

__call() method doesn't work correctly

I created class class.minmax.php

<?php
class minmax 
{
    public function __call($method, $arg)
    {
        if(!is_array($arg)) return false;
        $value = $arr[0];
        if ($method == "min")
        {
            for($i = 0; $i < count($arg); $i++)
            {
                if($arg[$i] < $value) $value = $arg[$i];
            }
        }

        if($method == "max")
        {
            for($i = 0; $i < count($arg); $i++)
            {
                if($arg[$i] > $value) $value = $arg[$i];
            }
        }

        return $value;
    }
}
?>

and tried to use it

<?php
require_once("class.minmax.php");

$obj = new minmax();
echo $obj->min(43, 18, 5, 23, 10, 56, 12);
echo "<br>";
echo $obj->max(41, 69, 45, 105, 28, 91);

?>

but as result I got only number 105 from max part of the method.
It is the example from the book and I don't understand why it doesn't work?

Upvotes: 1

Views: 110

Answers (1)

zavg
zavg

Reputation: 11061

You have a typo, change $value = $arr[0]; to $value = $arg[0];

Value $arr[0] does not exist within the function. So it is null compared with other elements of arguments array during the min function execution.

Upvotes: 4

Related Questions