Isn't this expression meaningless?

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 a reason they decided to build such a thing instead of just using it?

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

Any benefits?

+6
source share
2 answers

The IMHO programmer avoided call_user_func_array() for a reasonable number of typical calls to $instance->method() . Of course, it’s faster to use the method directly, rather than using call_user_func_array() . The code was written with love :)

+6
source

It is possible that there are functions with up to 4 arguments that expected to pass by value rather than by reference. Check out the feature documentation note regarding the use of PHP PHP 5.4. call_user_func_array docs

0
source

Source: https://habr.com/ru/post/989850/


All Articles