PHP magic method\uuuu调用,空实例变量

PHP magic method\uuuu调用,空实例变量,php,zend-framework2,instance-variables,magic-methods,Php,Zend Framework2,Instance Variables,Magic Methods,在对象上调用_invoke()时,我遇到了一个问题。__invoke()方法是否与实例变量无关?由于ZF2注入调用$this->getView()->render(…)(否则getView()返回null),我需要直接在我的模板上调用_invoke(),我希望在那里设置实例变量。有解决办法吗 请参阅我的代码: namespace Person\Person\View\Helper; use Zend\View\Helper\AbstractHelper; class PersonShowW

在对象上调用_invoke()时,我遇到了一个问题。__invoke()方法是否与实例变量无关?由于ZF2注入调用$this->getView()->render(…)(否则getView()返回null),我需要直接在我的模板上调用_invoke(),我希望在那里设置实例变量。有解决办法吗

请参阅我的代码:

namespace Person\Person\View\Helper;


use Zend\View\Helper\AbstractHelper;

class PersonShowWidget extends AbstractHelper
{

    protected $model = null;

    public function __construct(array $options = null)
    {
        $this->parseOptions($options);
    }

    public function __invoke()
    {

        var_dump($this->model); //returns null
        return $this->getView()->render('person/show/show_widget', array(
                'title' => 'Cliente',
                'model' => $this->model,
            )
        );
    }

    public function setOptions(array $options = null)
    {
        $this->parseOptions($options);
    }

    protected function parseOptions(array $options = null)
    {
        if (!is_null($options) && is_array($options)) {
            if (isset($options['model'])) {
                $model = $options['model'];
                if (isset($model['id'])) {
                    $this->model['id'] = $model['id'];
                } else {
                    throw new \Exception;
                }
                if (isset($model['form'])) {
                    $this->model['form'] = $model['form'];
                } else {
                    throw new \Exception;
                }
            }
        }

        var_dump($this->model); //returns valid data

    }
}
在调用uu invoke()之前,我确实使用一些选项或setOptions方法调用了构造函数


谢谢,

您必须使用工厂初始化视图帮助程序。通过这种方式,您可以确保在调用_invoke方法之前调用构造函数。不,_invoke()方法对实例变量不是不可知的

在Module.php中

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'personShowWidget' => function ($helpers) {
                $array = array();
                $helper = new Person\Person\View\Helper\PersonShowWidget($array);
                return $helper;
            },
        )
    );
}
或者在module.config.php中

'view_helpers' => array
(
    'factories' => array(
        'personShowWidget' => function ($helpers) {
            $array = array();
            $helper = new Person\Person\View\Helper\PersonShowWidget($array);
            return $helper;
        },
    )
)
在性能方面,您最好创建一个工厂类,而不是一个可调用的类。 更多信息:

编辑:

您似乎错误地使用了ViewHelper。您不必自己创建实例。只需在视图中使用ViewHelper。那么为什么不把
$options
作为
\u invoke
方法的参数呢

public function __invoke(array $options = null)
{
    $this->setOptions($options);

    return $this->getView()->render('person/show/show_widget', array(
            'title' => 'Cliente',
            'model' => $this->model,
        )
    );
}
在控制器中,将选项数组传递给视图:

return array(
    'options' => $options,
);
<?php echo $this->personShowWidget($this->options); ?>
并在视图中调用ViewHelper:

return array(
    'options' => $options,
);
<?php echo $this->personShowWidget($this->options); ?>

也许我在概念上做得不对,我试图在控制器中创建一个新的PersonShowWidget,通过setOptions方法配置它,然后在视图中显示(回显)。可能getView()方法返回null是因为我没有通过视图中的ViewManager?我是按照您现在提到的方式执行的,问题是如果我在控制器中创建viewhelper,则永远不会调用setView()。现在我正在尝试在控制器中渲染它,因为我想将它转换为一个模式,并且我只想在按下按钮时发送代码。解决方案: