Edit report at https://bugs.php.net/bug.php?id=64513&edit=1

 ID:                 64513
 Updated by:         vr...@php.net
 Reported by:        bugs dot php dot net at majkl578 dot cz
 Summary:            DateTimeImmutable is incompatible with DateTime and
                     leads to BC breaks
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Date/time related
 Operating System:   -
 PHP Version:        5.5.0beta1
-Assigned To:        
+Assigned To:        vrana
 Block user comment: N
 Private report:     N

 New Comment:

DateTimeImmutable doesn't inherit from DateTime in PHP 5.5.0RC1 anymore.


Previous Comments:
------------------------------------------------------------------------
[2013-03-25 17:19:02] 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



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



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64513&edit=1

Reply via email to