php - DateTime方法链接问题

标签 php

这有效:

$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。

最佳答案

变更日志

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

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

关于php - DateTime方法链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5497490/

相关文章:

c# - 一键智能搜索

php - 从虚拟/自定义 "web server"调用 PHP

php - 如何将我的 Azure 中的 Web 应用程序与 MySQL 数据库链接?

PHP:在 Switch 的情况下有两个值?

php - 这是wordpress病毒吗?

php - Laravel 5.4 路由相同的前缀组但不同的中间件给出错误

javascript - AJAX 请求 php 获取服务器上指定目录中的文件列表,通过 post/get 发送(没有 JQUERY)

PHP mySQL numRows() 用法和错误?

php - 当我打开它时显示一个空白网页

php - 在 Heroku 应用程序上将大文件导入 mysql