2013-04-17 52 views
1

我想知道是否可以在View Helper中使用多重函數?比方說,我有這樣的視圖助手:Zend Framework在視圖幫助器中使用多重函數

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); 

我試圖做到這一點,但不幸的是我有一個錯誤。我們鎖定在類名後面只使用方法名嗎?

感謝您的幫助

回答

1

我這樣做,只是返回$此構造函數:

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 { 
     ... 
    } 
} 
0

即使類工作這一呼籲可能不會:目前

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

試圖訪問一個公共成員(時間)的$這樣的:

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

現在會訪問convertMyDate ()幫手的方法時間()(至少在理論上)

它可能是最簡單和更靈活樂只是建立一個convertMyDate()助手,並用它喜歡:

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

因爲這樣做的東西你要查找的類型也可能會有所幫助看代碼的東西像HeadScript()幫手。

相關問題