ZF2批量复制phtml中的ViewModel变量

时间:2015-07-19 15:44:13

标签: php zend-framework2

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

Using $this when not in object context 

我想知道有没有办法可以批量复制Controller中定义的ViewModel的Variables容器数据结构中的所有变量和对象,作为局部变量和要查看的对象,以便我可以在本地函数中使用它们? / p>

换句话说,像someCreateVariablesFromViewModelArray方法一样在phtml中调用。因为我在Zend Framework 2中的ViewModel Variables容器数组中有很多数据。

1 个答案:

答案 0 :(得分:1)

听起来你有这样的事情:

//view + some html
<?php
    function something() {
        //do sth
    };
?>
<p>some text</p>

<?php something() ?>
  1. 我建议写一个view-helper。在那里,您可以访问视图或注入所需的组件。

  2. 您可以将$ this注入您的函数:

  3. //view + some html
    <?php
        function something($view) {
            //do sth
        };
    ?>
    <p>some text</p>
    
    <?php something($this) ?>
    
    1. 不知道它是如何调用的,但我确信这也是一个(不太好)的解决方案:
    2. <?php
          $something = function() use ($this) {
              //do sth
          };
      ?>