php类的实例化和调用,PHP:如何使用另一个类中的参数实例化一个类

我处于某种情况,我需要使用另一个类的实例中的参数实例化一个类.

这是原型:

//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开发人员来说,以上不应该是很大的问题.请帮忙.

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值