|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[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
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
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