Now I must admit that what follows is pretty useless, but I had some spare time so I decided to to do it anyway.
Since version 5.6 PHP supports a new Splat operator. Among other things you can use it instead of the call_user_func_array function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
call_user_func_array('do_something', $params);
//Will now become
do_something(...$params);
1
This new syntax obviously looks much better and also lets you use IDE features. But does it perform any better ? just for fun first let's check the opcodes:
1
call_user_func_array('test', $params);
/**
2 0 > SEND_VAL 'test'
1 SEND_VAR !0
2 DO_FCALL 2 'call_user_func_array'
4 3 > RETURN 1
*/
test(...$params);
/**
2 0 > INIT_FCALL_BY_NAME 'test'
1 <165> !0
2 DO_FCALL_BY_NAME 0
3 3 > RETURN 1
*/
To be honest that wasn’t very useful =) It basically tells us the same thing that was already obvious, the difference in performance is going to be the overhead of calling call_user_func_array. So now let;s run some loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function test($a, $b, $c)
{
}
$params = array(1,2,3);
$t = time();
for($i=0; $i<100000000; $i++)
call_user_func_array('test', $params);
echo time()-$t."\n";
$t = time();
for($i=0; $i<100000000; $i++)
test(...$params);
echo time()-$t."\n";
/**
49
13
*/
So using the splat operator is about 3 times faster! Obviously we’re talking nanoseconds here so having a few call_user_func calls makes practically 0 difference. And if your code heavily relies on it, you’re probavly doing it wrong in the first place.
Hope you enjoyed this small trip into the world of useless benchmarks =)