php动态类建设 [英] Php dynamic class construction

查看:96
本文介绍了php动态类建设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图避免使用eval.我可以像这样动态地实例化一个类:

I'm trying to avoid the use of eval. I can dynamically instantiate a class like this:

class myclass {}

$my_class_name = 'myclass';
$obj = new $myclass();

如果构造函数如下:

class myclass {
    public function __construct( $argument1, $argument2 ) {}
}

并且我在数组中具有参数的值,如何动态实例化类将其传递给动态参数?请注意,我无法修改该类,因此我必须努力使用它.

and i have the values of the arguments in an array, how can i dynamically instantiate the class and pass it dynamic arguments? Mind that I have no way to modify the class, so I have to work on the way of using it.

谢谢

推荐答案

ReflectionClass::newInstanceArgs seems to be just the thing you're looking for.

这篇关于php动态类建设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆