【问题标题】:ZF2 Bulk copy ViewModel variables in phtmlZF2 在 phtml 中批量复制 ViewModel 变量
【发布时间】:2015-07-20 04:40:45
【问题描述】:

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

Using $this when not in object context 

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

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

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    听起来你有这样的东西:

    //view + some html
    <?php
        function something() {
            //do sth
        };
    ?>
    <p>some text</p>
    
    <?php something() ?>
    
    1. 我建议写一个view-helper。在那里你可以访问视图或注入你需要的组件。

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

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

    【讨论】:

    • 不是我正在寻找的那个,但它拯救了我的胆量。谢谢。
    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2011-05-03
    • 2012-09-15
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多