Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlrdw's avatar
Level 75

Learning from Taylor studying vendor folder PHP general

I notice Taylor uses both these techniques:

public static function __callStatic($method, $params)
    {
        $instance = \Model\Dog::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    }

and

public static function __callStatic($method, $params)
    {
        $instance = \Model\Dog::class;
       return call_user_func_array(array($instance, $method), $params);
    }

If I use one of these like

use Model\DBcall\DBdog as db;

then

$dogrows = db::dogCountadmin($dogsearch, $aval);

Both of the first two methods I showed work perfect. But which is better to use in modern PHP,

return $c->$method(...array_values($params));  

//OR

return call_user_func_array(array($instance, $method), $params);

First one, second, either.

Thanks.

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

They are both the same thing. One is just shorter because it uses the newer php splat [...] operator with variadic functions. That was introduced in php5.6, so I'm sure there's a lot of laravel code that predates 5.6 still using call_user_func_array(). So to answer your question, I'd go with the way with the least amount of code (splat operator).

Variadic functions can now be implemented using the ... operator, instead of relying on func_get_args().

http://php.net/manual/en/migration56.new-features.php

1 like
jlrdw's avatar
Level 75

Thanks @cronix Yeah I figured the splat, but it's like torn between the two.

I have read and read and read about call_user_func_array, it works, but behind the scenes it is magic, I am yet to figure out what goes on that makes it work.

1 like

Please or to participate in this conversation.