在create_function上使用$this时出错


error using $this on create_function

当我在create_function中使用$this时,我得到一个错误

致命错误:不在对象上下文中时使用$this

create_function('$model, $this', 'return $this->di->getStatus($model->getStatus)');

后来我把它叫做

$function($model, $this);

我向函数传递了$this,但似乎不起作用。有办法绕过它吗?

感谢

经过数小时的调试和研究,我发现我的代码被封装在一个函数中,所以最终的结果类似于

function($var1, $var2) { return create_function()}

通过这种方式创建了一个闭包,$this不再在我想要的范围内。

更多关于闭包的信息,请点击此处:http://php.net/manual/ro/class.closure.php