I benchmarked the comparison in speed between variable functions, call_user_func, and eval. My results are below:
Variable functions took 0.125958204269 seconds.
call_user_func took 0.485446929932 seconds.
eval took 2.78526711464 seconds.
This was run on a Compaq Proliant server, 180MHz Pentium Pro 256MB RAM. Code is as follows:
<?php
function fa () { return 1; }
function fb () { return 1; }
function fc () { return 1; }
$calla = 'fa';
$callb = 'fb';
$callc = 'fc';
$time = microtime( true );
for( $i = 5000; $i--; ) {
$x = 0;
$x += $calla();
$x += $callb();
$x += $callc();
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "Variable functions took " . (microtime( true ) - $time) . " seconds.<br />" );
$time = microtime( true );
for( $i = 5000; $i--; ) {
$x = 0;
$x += call_user_func('fa', '');
$x += call_user_func('fb', '');
$x += call_user_func('fc', '');
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "call_user_func took " . (microtime( true ) - $time) . " seconds.<br />" );
$time = microtime( true );
for( $i = 5000; $i--; ) {
$x = 0;
eval( '$x += ' . $calla . '();' );
eval( '$x += ' . $callb . '();' );
eval( '$x += ' . $callc . '();' );
if( $x != 3 ) die( 'Bad numbers' );
}
echo( "eval took " . (microtime( true ) - $time) . " seconds.<br />" );
?>