如何创建具有给定参数数组的类以发送到构造函数?类似于以下内容:
class a {
var $args = false;
function a() {$this->args = func_get_args();}
}
$a = call_user_func_array('new a',array(1,2,3));
print_r($a->args);
理想情况下,这需要在 PHP4 和 PHP5 中工作,无需修改类。有任何想法吗?
ReflectionClass:newInstance()(或 newInstanceArgs())让我们来做。
例如
class Foo {
public function __construct() {
$p = func_get_args();
echo 'Foo::__construct(', join(',', $p), ') invoked';
}
}
$rc = new ReflectionClass('Foo');
$foo = $rc->newInstanceArgs( array(1,2,3,4,5) );
编辑:没有 ReflectionClass 并且可能与 php4 兼容(抱歉,目前没有 php4)
class Foo {
public function __construct() {
$p = func_get_args();
echo 'Foo::__construct(', join(',', $p), ') invoked';
}
}
$class = 'Foo';
$rc = new $class(1,2,3,4);
速度比较:由于已经提到了反射速度,这里有一个小(综合)测试
define('ITERATIONS', 100000);
class Foo {
protected $something;
public function __construct() {
$p = func_get_args();
$this->something = 'Foo::__construct('.join(',', $p).')';
}
}
$rcStatic=new ReflectionClass('Foo');
$fns = array(
'direct new'=>function() { $obj = new Foo(1,2,3,4); },
'indirect new'=>function() { $class='Foo'; $obj = new $class(1,2,3,4); },
'reflection'=>function() { $rc=new ReflectionClass('Foo'); $obj = $rc->newInstanceArgs( array(1,2,3,4) ); },
'reflection cached'=>function() use ($rcStatic) { $obj = $rcStatic->newInstanceArgs( array(1,2,3,4) ); },
);
sleep(1);
foreach($fns as $name=>$f) {
$start = microtime(true);
for($i=0; $i<ITERATIONS; $i++) {
$f();
}
$end = microtime(true);
echo $name, ': ', $end-$start, "\n";
sleep(1);
}
打印在我的(不是那么快)笔记本上
direct new: 0.71329689025879
indirect new: 0.75944685935974
reflection: 1.3510940074921
reflection cached: 1.0181720256805
那不是很糟糕,是吗?
来自维基百科:
工厂方法模式是一种面向对象的设计模式。与其他创建模式一样,它处理创建对象(产品)的问题,而不指定将创建的对象的确切类别。
如果您不想为此使用专用工厂,您仍然可以将 Volker 的代码包装到一个函数中,例如
/**
* Creates a new object instance
*
* This method creates a new object instance from from the passed $className
* and $arguments. The second param $arguments is optional.
*
* @param String $className class to instantiate
* @param Array $arguments arguments required by $className's constructor
* @return Mixed instance of $className
*/
function createInstance($className, array $arguments = array())
{
if(class_exists($className)) {
return call_user_func_array(array(
new ReflectionClass($className), 'newInstance'),
$arguments);
}
return false;
}