Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't this switch statement a nonsense?

I found this weird switch statement in Laravel 5 core:

switch (count($args)) {
    case 0:
        return $instance->$method();
    case 1:
        return $instance->$method($args[0]);
    case 2:
        return $instance->$method($args[0], $args[1]);
    case 3:
        return $instance->$method($args[0], $args[1], $args[2]);
    case 4:
        return $instance->$method($args[0], $args[1], $args[2], $args[3]);
    default:
        return call_user_func_array([$instance, $method], $args);

Is there any reason why they possibly decided to build such a thing instead of just using this?

return call_user_func_array([$instance, $method], $args);

Any benefits?

like image 789
Robo Robok Avatar asked Jun 27 '15 07:06

Robo Robok


1 Answers

IMHO the programmer avoided to call_user_func_array() for a reasonable amount of typical calls to $instance->method(). Of course it is faster to call the method directly instead of using call_user_func_array(). The code was written with love :)

like image 148
hek2mgl Avatar answered Oct 04 '22 03:10

hek2mgl