2011-03-31 70 views
1

這工作:DateTime的方法鏈問題

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

但這:

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

給了我這個錯誤:

Fatal error: Call to a member function format() on a non-object in ... on line ...

Fatal error (shutdown): Call to a member function format() on a non-object in ... on line ...

我已經看過了文檔中的DateTime::modify方法它的回報值是這樣的:

Returns the DateTime object for method chaining or FALSE on failure.

爲什麼方法鏈不起作用?

我使用PHP版本5.2.6。

回答

6

更新日誌

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

於是久違的鏈接DateTime對象在PHP 5.3.0

+0

只介紹我怎麼能不看到這一點。 – 2011-03-31 09:14:05

+0

容易被忽視,除非你之前被它蜇過 – 2011-03-31 09:32:04