I was wondering about what the best practices are when it comes to call a class method using the above functions to fill up a method call dynamically using an array!
What advantages and disadvantages do I have? I mean, it seems the RefelectionMethod + invokeArgs option is up to 40% faster than the call_user_funcion in certain conditions… but do I miss something?
thanks.
As requested i will add a little benchmark of my scenario where i needed refereces to be passed... i know its a very specific case and that this is not stricly related to the question above!
class foo { public function bar(&$a, &$b, &$c) { /* */ } }
$t1 = microtime(true);
$arr = array(1,2,3);
$foo = new foo;
$rfl = new ReflectionMethod('foo', 'bar');
for ($i=0; $i < 10000; ++$i)
{
$rfl->invokeArgs($foo, $arr);
}
$t2 = microtime(true);
echo sprintf("\nElapsed reflectionmethod : %f", $t2 - $t1);
$t1 = microtime(true);
$arr = array(1,2,3);
$foo = new foo;
for ($i=0; $i < 10000; ++$i)
{
foreach( $arr as $k => $v ) $ref[$k] = &$arr[$k];
call_user_func_array( array($foo, 'bar'), $arr);
}
$t2 = microtime(true);
echo sprintf("\nElapsed calluserfuncarray : %f", $t2 - $t1);
the result
Elapsed reflectionmethod : 0.025099
Elapsed calluserfuncarray : 0.051189
I really would just like to know when its better to use one versus the other, and why! its not strictly related to speed though!