Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime method chaining problem

Tags:

php

This works:

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

But this:

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

Gives me this error:

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 ...

I have looked up the DateTime::modify method in the documentation and its return value is this:

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

Why is method chaining not working?

I am using PHP version 5.2.6.

like image 959
Richard Knop Avatar asked Apr 12 '26 02:04

Richard Knop


1 Answers

Changelog

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

So returning the DateTime object for chaining was only introduced in PHP 5.3.0

like image 143
Mark Baker Avatar answered Apr 13 '26 16:04

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!