ZF2在php中批量复制ViewModel变量



在viewphtml文件中访问本地函数中的变量时,我得到了这个流行的错误

Using $this when not in object context 

我想知道是否有任何方式,我可以批量复制所有变量和对象在控制器中定义的ViewModel的变量容器数据结构,作为局部变量和对象来查看,所以我可以在局部函数中使用它们?

换句话说,就像phtml中调用的someCreateVariablesFromViewModelArray方法。因为我有很多数据在ViewModel变量容器数组在Zend框架2.

听起来像这样:

//view + some html
<?php
    function something() {
        //do sth
    };
?>
<p>some text</p>
<?php something() ?>
  1. 我建议编写一个视图帮助器。

  2. 你可以把$this注入到你的函数中:

//view + some html
<?php
    function something($view) {
        //do sth
    };
?>
<p>some text</p>
<?php something($this) ?>
  • 不知道它是怎么叫的,但我确信这也是一个(不太好的)解决方案:
  • <?php
        $something = function() use ($this) {
            //do sth
        };
    ?>
    

    最新更新