我处于某种情况,我需要使用另一个类的实例中的参数实例化一个类.
这是原型:
//test.php
class test
{
function __construct($a, $b, $c)
{
echo $a . '
';
echo $b . '
';
echo $c . '
';
}
}
现在,我需要使用下面的类cls函数来实例化上面的类:
class myclass
{
function cls($file_name, $args = array())
{
include $file_name . ".php";
if (isset($args))
{
// this is where the problem might be, i need to pass as many arguments as test class has.
$class_instance = new $file_name($args);
}
else
{
$class_instance = new $file_name();
}
return $class_instance;
}
}
现在,当我尝试创建测试类的实例,同时传递参数:
$myclass = new myclass;
$test = $myclass->cls('test', array('a1', 'b2', 'c3'));
它给出错误:
缺少参数1和2;只有第一个参数通过.
如果我实例化一个在它的构造函数中没有参数的类,这个工作正常.
对于经验丰富的PHP开发人员来说,以上不应该是很大的问题.请帮忙.
谢谢