FOTD: ReflectionClass()->newInstanceArgs($args) is “slow”

(FOTD = Finding of the day :))

For okapi we needed to have a function which loads any class with any number of arguments. This is not so easy in PHP as it looks like and the way to do this until 5.1.3 was something like:

Looks ugly, but works, as long as you have as many “case” statements as you have arguments

Since 5.1.3 you can use the reflection extension to do this much nicer

But as my gut said that ReflectionClass is not the fastest thing on earth, I made some benchmarks and indeed:

The ugly switch statement is almost twice as fast as the one with the reflection class. But even more surprising (to me) was that both are three times faster in PHP 5.3. I’m now even more looking forward to the final release of PHP 5.3.0 :)

BTW, we decided nevertheless for the reflection approach, as it is way less ugly and the performance difference will be negligible. And the unit for the figures in the benchmark is not really important, the relative comparison is what’s interesting.

i have a similar method in my framework. it works like a charm. not only is the switch statement ugly, it will also fail, if you accidently implement something, which requires even more parameters.

of course you could just write down 100 cases, but this really would be a candidate for the daily WTF!? ;)

eval may be evil, but it sure can be useful…:

$name = ‘bar’;
$init[] = ‘baz’;
$init[] = ‘booze’;

$vars = ‘”‘.implode(‘”,”‘,$init).'”‘;

$eval = “$ob = new $name($vars); “;


class bar{
public function __construct($i=’not i’,$j=’not j’,$k=’not k’){
echo “$i

What about using call_user_func_array ?

return call_user_func_array(array($name), $vars);

Or something to that effect :)

@Rob: eval is evil and your example will only work with strings, bad…

@Chris call_user_func_array works with static method well, not really with __construct.

won’t do what you want.

I ended up with something similar to what we do in JS ;-)

Of course, you won’t be able to write normal PHP classes anymore, but maybe this syntax: new Object(array(a => 1, b => 2)); can be interesting to have. Yes, it’s Pythonic ;-)

I also use reflection for creating instances dynamicly in my OOP based framework. Works well, and the time spent is in end probably worth it.

@Rob: Why not create a function for that? That also supports objects…

function CreateInstance($name, Array $argv)
$args = “$argv[” . implode(“], $argv[“, array_keys($argv)) . “]”;
return class_exists($name) ? eval(“return new {$name}({$args});”) : false;

Not tested, but should get the job done.. A little more securly too ;-)

Really nice that this blog removes the backward slashes in the code(sarcastic). Function will most likely _not_ function without them since vars will be evaluated directly instead of (as desired) when passed through Eval().

function CreateInstance($name, Array $argv)
$args = “(backward slash)$argv[” . implode(“], (backward slash)$argv[“, array_keys($argv)) . “]”;
return class_exists($name) ? eval(“return new {$name}({$args});”) : false;

So for those geniouses out there.. Replace “(backward slash)” with a real backward slash..

F- me for being so nice ;)


CreateInstance(“myClass”, array(null, new stdClass()));


I thought call_user_func_array would use the constructor if you didn’t supply a proper array but it complains.

However saying it only works with static methods is wrong.

@Yoan: Can’t see how the function would fail.

> However saying it only works with static methods is wrong.

Yep, right. Double right actually, all my apologies. What about the perfs of eval?

Gist is updated ( and reflection looks worser than eval. Anyone to run this upon PHP 5.3 ?


Not worse, just a little bit better. Sorry (now I stop flooding here)

Hi Guys,

After refactoring ReflectionClass::newInstanceArg() i found this Blog-Post. Check out this similar version which works not only with string arguments: