Robo Robok
Robo Robok

Reputation: 22683

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?

Upvotes: 6

Views: 173

Answers (2)

hek2mgl
hek2mgl

Reputation: 157967

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 :)

Upvotes: 6

Heath Malmstrom
Heath Malmstrom

Reputation: 80

It's possible that there were functions with up to 4 arguments that expected pass by value instead of pass by reference. Notice the note on the function documentation about pre PHP 5.4 usage. call_user_func_array docs

Upvotes: 0

Related Questions