3

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?

1
  • Remember to accept an answer, if it helps you with your problem. This refers to your other questions too. Commented May 31, 2011 at 20:18

2 Answers 2

7
$reflection = new ReflectionClass($classname);
$object = $reflection->newInstanceArgs($args);

http://php.net/reflection

Sign up to request clarification or add additional context in comments.

2 Comments

Before reflection, I had this exact same problem. Know what I did? Generated a string and eval'd it. Disgusting.
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.
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.