2014-05-05 61 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