php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64513 DateTimeImmutable is incompatible with DateTime and leads to BC breaks
Submitted: 2013-03-25 17:19 UTC Modified: 2013-05-15 23:03 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:3 (75.0%)
From: bugs dot php dot net at majkl578 dot cz Assigned: vrana (profile)
Status: Closed Package: Date/time related
PHP Version: 5.5.0beta1 OS: -
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bugs dot php dot net at majkl578 dot cz
New email:
PHP Version: OS:

 

 [2013-03-25 17:19 UTC] bugs dot php dot net at majkl578 dot cz
Description:
------------
PHP 5.5 adds DateTimeImmutable, which extends DateTime.

As Benjamin Eberlei already pointed out in internals list [1], the behavior is not compatible with each other and therefore the inheritance seems to be wrong.
This bad implementation is not backward compatible and may lead to serious problems with existing code.

Here are some examples which would get broken (bad behavior or even infinite loop!) by passing DateTimeImmutable instead of DateTime.


function testOne(DateTime $dt)
{
	for ($i = 1; $i <= 2; $i++, $dt->modify('first day of next month')) {
		echo $dt->format('Y/m/d'), PHP_EOL;
	}
}

testOne(new DateTime());
// 2013/03/25
// 2013/04/01

testOne(new DateTimeImmutable());
// 2013/03/25
// 2013/03/25

-------------------

function testTwo(DateTime $from, DateTime $to)
{
	for ($current = clone $from; $current <= $to; $current->modify('+ 1 day')) {
		echo $current->format('Y/m/d'), PHP_EOL;
	}
}

testTwo(new DateTime(), new DateTime('+ 1 day'));
// 2013/03/25
// 2013/03/26

testTwo(new DateTimeImmutable(), new DateTimeImmutable('+ 1 day'));
// 2013/03/25
// 2013/03/25
// 2013/03/25
// infinite loop occurs!



This clearly shows what side-effects might this "feature" have.

Please, do not add another badly designed feature and revert it before 5.5 gets released.


[1] http://marc.info/?l=php-internals&m=136135370215794&w=2


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-15 23:03 UTC] vrana@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: vrana
 [2013-05-15 23:03 UTC] vrana@php.net
DateTimeImmutable doesn't inherit from DateTime in PHP 5.5.0RC1 anymore.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC