ZF2批量复制phtml中的ViewModel变量

While accessing variables in local function within view phtml file I get this popular error

Using $this when not in object context 

I want to know is there any way by which I can bulk copy all the variables and objects in Variables container data structure of ViewModel defined in Controller, as local variables and objects to view so I can use them in local functions?

In otherwords, like someCreateVariablesFromViewModelArray method to call in phtml. Because I have lot of data in ViewModel Variables container array in Zend Framework 2.

It sounds like you have something like this:

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

<?php something() ?>
  1. I recommend writing a view-helper. There you have access to the view or can inject components you need.

  2. You can inject $this into your function:

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

<?php something($this) ?>
  1. Don't know how it is called but I am sure this is also a (not so good) solution:
<?php
    $something = function() use ($this) {
        //do sth
    };
?>