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?
Asked
Active
Viewed 576 times
3

guai
- 780
- 1
- 12
- 29
-
Remember to accept an answer, if it helps you with your problem. This refers to your other questions too. – KingCrunch May 31 '11 at 20:18
2 Answers
7
$reflection = new ReflectionClass($classname);
$object = $reflection->newInstanceArgs($args);

KingCrunch
- 128,817
- 21
- 151
- 173
-
Before reflection, I had this exact same problem. Know what I did? Generated a string and eval'd it. Disgusting. – erisco May 31 '11 at 20:02
-
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. – KingCrunch May 31 '11 at 20:05
-1
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.