PHP program to demonstrate the method overloading based on the number of arguments

bookmark

<?php
//PHP program to demonstrate the method overloading
//based on the number of arguments.
class Sample
{
    function __call($function_name, $args)
    {

        if ($function_name == 'sum')
        {
            switch (count($args))
            {
                case 2:
                    return $args[0] + $args[1];
                case 3:
                    return $args[0] + $args[1] + $args[2];
            }
        }
    }
}

$obj = new Sample();

printf("Sum: " . $obj->sum(10, 20) . "<br>");
printf("Sum: " . $obj->sum(10, 20, 30) . "<br>");
?>


Output
Sum: 30
Sum: 60