Re: How to dynamically call a constructor like call_user_func_array
| From: | Matthew Weier O'Phinney | Date: | Wed, 03 Dec 2008 14:07:47 +0000 |
| Subject: | Re: How to dynamically call a constructor like call_user_func_array | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-51204@lists.php.net to get a copy of this message | ||
On Thu, Nov 27, 2008 at 7:23 AM, <jtaal@eljakim.nl> wrote:
> I want to create a new instance of a class (which name I don't know) and I
> want to expand a given array to be the constructor's parameters:
>
> class mycls {
> function __construct($var1, $var2) {
> }
> }
>
> $clsname = 'mycls';
> $val = array('value1', 'value2');
> $obj = new mycls($val[0], $val[1]); // This line needs to be dynamic.
>
> Couldn't find on the php site how to do this.
The only way to do it is via Reflection.
$r = new ReflectionClass($clsname);
$obj = $r->newInstanceArgs($val);
--
Matthew Weier O'Phinney
mweierophinney@gmail.com
http://weierophinney.net/matthew/