【问题标题】:DateTime method chaining problemDateTime 方法链接问题
【发布时间】:2011-03-31 09:06:56
【问题描述】:

这行得通:

$aWeekAfterDate = new DateTime(); 
$aWeekAfterDate->modify('+1 week');
$aWeekAfterDate->format('d.m.Y');

但是这个:

$aWeekAfterDate = new DateTime(); 
$aWeekAfterDate->modify('+1 week')->format('d.m.Y');

给我这个错误:

致命错误:调用成员函数 format() 在...中的非对象上 线...

致命错误(关闭):调用 成员函数 format() 上 non-object in ...在线...

我在文档中查找了DateTime::modify方法,它的返回值是这样的:

返回方法的 DateTime 对象 失败时链接或 FALSE。

为什么方法链不起作用?

我使用的是 PHP 5.2.6 版。

【问题讨论】:

    标签: php


    【解决方案1】:

    更新日志

    Version   Description 
    5.3.0     Changed the return value on success from NULL to DateTime. 
    

    因此返回 DateTime 对象进行链接仅在 PHP 5.3.0 中引入

    【讨论】:

    • 很容易被忽视,除非你之前被它刺痛过
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多