【问题标题】:Zend Framework use mulitple method in a View helperZend Framework 在视图助手中使用多种方法
【发布时间】:2013-04-09 15:40:24
【问题描述】:

我想知道是否可以通过 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);

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

感谢您的帮助

【问题讨论】:

    标签: zend-framework view methods helper


    【解决方案1】:

    我这样做了,然后在构造函数中简单地返回 $this:

    public function time() {
         return $this;
    }
    
    public function convertMyDate($some_date) {
        ...
    }
    

    然后:

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

    如果你想保留$this->time($some_date),那么你可以这样做(虽然我认为一个新的方法更好):

    public function time($time = false) {
        if(!$time)
           return $this;
        else {
           ...
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

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

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

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

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

      查看类似 HeadScript() 帮助程序的代码也可能会有所帮助,因为它可以满足您的需求。

      【讨论】:

        猜你喜欢
        • 2011-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-16
        • 1970-01-01
        相关资源
        最近更新 更多