2014-05-05 65 views
-2

我正在使用名爲ZosoHelper的自定義幫助器。我使用名爲get_relative_time的函數來格式化給定的日期。我有另一個功能,在同一個ZosoHelper中調用get_month,我想在我的get_relative_time函數中訪問它。但我無法讓它工作。CakePHP幫助函數使用其他函數(在同一個幫助器中)

我試過這樣的事情:(修剪 - 只爲邏輯)

class ZosoHelper extends AppHelper { 

    function get_month(){ 
     $month = "januari"; 
     return $month; 
    } 

    function get_relative_time(){ 
     $date = $this->Zoso->get_month(); 
     //Note: I also tried $this->get_month(); 
     return $date; 
    } 

} 

我打消了我所有的代碼,以保持它的簡單。我只想在我的新get_relative_time函數中調用我的其他函數(get_month)。

+4

'$ this-> get_month()'應該可以工作 – arilia

+4

Basic OOP PHP。順便說一句,你應該遵循CakePHP的編碼約定。方法是駝背,並始終具有可見性(公開...)。 – mark

+0

你的評論說「注意:我也試過$ this-> get_month();」這肯定應該工作。你有錯誤或通知? – Nunser

回答

0
Try...... 
This is working and return januari. 
<?php 
class ZosoHelper extends AppHelper { 

    function get_month(){ 
     $month = "januari"; 
     return $month; 
    } 

    function get_relative_time(){ 
     $date = $this->get_month(); 
     return $date; 
    } 

} 
?> 
+0

正如我在上面的評論中(在我的問題中)所說的那樣:這是同一行上的另一個錯誤。 $ this-> get_month()確實有效。 – Femke