在view phtml文件中访问本地函数中的变量时,我得到了这个流行的错误
Using $this when not in object context
我想知道有没有办法可以批量复制Controller中定义的ViewModel的Variables容器数据结构中的所有变量和对象,作为局部变量和要查看的对象,以便我可以在本地函数中使用它们? / p>
换句话说,像someCreateVariablesFromViewModelArray方法一样在phtml中调用。因为我在Zend Framework 2中的ViewModel Variables容器数组中有很多数据。
答案 0 :(得分:1)
听起来你有这样的事情:
//view + some html
<?php
function something() {
//do sth
};
?>
<p>some text</p>
<?php something() ?>
我建议写一个view-helper。在那里,您可以访问视图或注入所需的组件。
您可以将$ this注入您的函数:
//view + some html
<?php
function something($view) {
//do sth
};
?>
<p>some text</p>
<?php something($this) ?>
<?php
$something = function() use ($this) {
//do sth
};
?>