I have string containing classname and an array of args.
$classname($args) works, but in this case I have only 1 argument in constructor
Anyone know, how to do this with args expanded?
-
Remember to accept an answer, if it helps you with your problem. This refers to your other questions too.KingCrunch– KingCrunch2011-05-31 20:18:10 +00:00Commented May 31, 2011 at 20:18
Add a comment
|
2 Answers
$reflection = new ReflectionClass($classname);
$object = $reflection->newInstanceArgs($args);
2 Comments
erisco
Before reflection, I had this exact same problem. Know what I did? Generated a string and eval'd it. Disgusting.
KingCrunch
yes, I don't see another solution without reflection too. However, when using interfaces, the factory-pattern (or such), something like this is usually not required.
Haha I just asked this question: PHP passing a class as a reference?
And you can't expand the $args. You could try passing $args[0], $args[1] etc.