ZF2插件返回值给出了一个Object无法转换为字符串


ZF2 plugin return value gives a Object could not be converted to string

我构建自己的插件只是为了学习框架。它只是返回一个日期()。当我试图回显它时,我的视图中出现了一个错误。理解这个错误并不难,但困难的是(或者至少,我认为困难的是)我应该如何从插件中返回它,这样我的视图(以及后来的其他视图)就不需要任何额外的信息,只需要pluginReturn?>

该插件已在module.config.php中注册(它可以工作,否则我不会出错)。

嗯,一些代码:

插件:

<?php
namespace Tijdmachine'Controller'Plugin;
use Zend'Mvc'Controller'Plugin'AbstractPlugin;
class TellTheTime extends AbstractPlugin{
    /**
    * @return string
    */
    public function giveMyTime(){
        return date('H:i:s');
    }
}

来自相关控制器的线路:

$plugin = $this->TellTheTime();
        $plugin->giveMyTime();
        $views = new ViewModel(array('text' => 'Het is nu de tijd.', 'pluginReturn' => $plugin));

视图行:

<?=$this->pluginReturn?>

我得到的错误:可捕获的致命错误:类Tijdmachine''Controller''Plugin''TellTheTime的对象无法在第19行的/home/snoech/code/zftuts/trunk/module/Tijdmine/view/tijdmine/index/index.phtml中转换为字符串

您的$plugin包含该对象。重新分配变量中的值并将其提供给视图:

$plugin = $this->TellTheTime();
$myDate = $plugin->giveMyTime();
$views = new ViewModel(array('text' => 'Het is nu de tijd.', 'pluginReturn' => $myDate));

$plugin = $this->TellTheTime();
$views = new ViewModel(array('text' => 'Het is nu de tijd.', 'pluginReturn' => $plugin->giveMyTime()));