【发布时间】:2013-09-18 12:28:01
【问题描述】:
OOP 菜鸟在这里。我想利用 CakePHP 2.3 的短日期方法,以便在适当的时候获取“今天”或“昨天”,同时在输出不是“今天”或“昨天”时更改日期格式。
示例视图:echo $this->Time->niceShort($user['User']['last_invited_on'];.
我在lib/Cake/Utility/CakeTime.php 中找到了以下内容(Cake 的TimeHelper 使用CakeTime 实用程序。):
class CakeTime {
/**
* The format to use when formatting a time using `CakeTime::nice()`
*
* The format should use the locale strings as defined in the PHP docs under
* `strftime` (http://php.net/manual/en/function.strftime.php)
*
* @var string
* @see CakeTime::format()
*/
public static $niceFormat = '%a, %b %eS %Y, %H:%M';
/**
* The format to use when formatting a time using `CakeTime::timeAgoInWords()`
* and the difference is more than `CakeTime::$wordEnd`
*
* @var string
* @see CakeTime::timeAgoInWords()
*/
public static $wordFormat = 'j/n/y';
/**
* The format to use when formatting a time using `CakeTime::niceShort()`
* and the difference is between 3 and 7 days
*
* @var string
* @see CakeTime::niceShort()
*/
public static $niceShortFormat = '%B %d, %H:%M';
我能否以某种方式覆盖此类的公共属性,以便在视图中使用 Time->niceShort 时更改输出的日期格式? (这是“猴子补丁”吗?)如果是这样,什么是好的/干净的方法?
或者我应该编写一个扩展CakeTime 的新类,这是否意味着必须将视图中的$this->Time 更改为$this->MyNewSpiffingCustomisedTime(我不想像其他习惯使用Cake 的@ 的人那样做987654330@ 正在从事该项目)?我想知道这是否只是为了更改属性而过大。
【问题讨论】:
-
不适合更改库代码。创建您的课程并基本上扩展
-
使用 cakephp 内置助手创建新助手并扩展新助手..
标签: php cakephp datetime overriding