1

我想知道是否可以通过 View Helper 使用多个功能?假设我有这个视图助手:

class Zend_View_Helper_Time extends Zend_View_Helper_Abstract {

    public function time($date) {
        // Some code here
    }
}

在我看来,我会这样使用它:

$this->time($some_date);

但是,如果我愿意,在同一个助手中可以调用另一个方法,例如:

$this->Time->convertMyDate($some_date);

我试图这样做,但不幸的是我遇到了一个错误。我们是否锁定为仅在类名之后使用方法名?

谢谢你的帮助

4

2 回答 2

1

I do this, and simply return $this in the constructor:

public function time() {
     return $this;
}

public function convertMyDate($some_date) {
    ...
}

Then:

$this->time()->convertMyDate();

If you want to keep $this->time($some_date), then you could do as follows (although I think a new method is nicer):

public function time($time = false) {
    if(!$time)
       return $this;
    else {
       ...
    }
}
于 2013-04-17T06:46:56.810 回答
0

即使班级工作了这个电话也可能不会:

$this->Time->convertMyDate($some_date);

当前正在尝试访问 $this 的公共成员(时间):

$this->time()->convertMyDate($some_date);

现在将访问助手time()的convertMyDate()方法(至少在理论上)

构建一个 convertMyDate() 帮助器并像这样使用它可能是最简单和更灵活的:

$this->convertMyDate($this->time($some_date));

查看诸如帮助程序之类的代码也可能会有所帮助,HeadScript()因为它会执行您正在寻找的事情的类型。

于 2013-04-17T08:16:32.407 回答