Agr*_*jag 32 php arrays constructor arguments function
通常,如果我想将$ myarray中的参数传递给$ somefunction,我可以在php中使用
call_user_func_array($somefunction, $myarray);
Run Code Online (Sandbox Code Playgroud)
但是,当希望调用的函数是对象的构造函数时,这不起作用.出于相当明显的原因,它无法做到:
$myobj = new call_user_func_array($classname, $myarray);
Run Code Online (Sandbox Code Playgroud)
有什么相当优雅的确有效吗?
Gor*_*don 65
您可以使用Reflection API:
ReflectionClass::newInstanceArgs - 从给定的参数创建一个新的类实例.例:
$reflector = new ReflectionClass('Foo');
$foo = $reflector->newInstanceArgs(array('foo', 'bar'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14633 次 |
| 最近记录: |