Bug #80047 [NEW]: DatePeriod doesn't support custom DateTimeImmutable

From: Date: Thu, 03 Sep 2020 12:01:07 +0000
Subject: Bug #80047 [NEW]: DatePeriod doesn't support custom DateTimeImmutable
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228859@lists.php.net to get a copy of this message
From: oognic at gmail dot com Operating system: PHP version: 7.2.33 Package: Date/time related Bug Type: Bug Bug description:DatePeriod doesn't support custom DateTimeImmutable Description: ------------ If we create a custom class that inherits from DateTimeImmutable, this class can be given as parameter to DatePeriod. However, when iterating or trying to get the start or end dates, DatePeriod will try to return instances of our custom class but without using its constructor, which will produce incoherent objects. See this PR for more details: https://github.com/thecodingmachine/safe/pull/227/files Test script: --------------- <?php class CustomDateTimeImmutable extends \DateTimeImmutable { private $foo; public function __construct($time = "now", $timezone = NULL) { parent::__construct($time, $timezone); $this->foo = "foo"; } public function getFoo() { return $this->foo; } } $datePeriod = new \DatePeriod(new CustomDateTimeImmutable('2020-01-01'), new \DateInterval('P1D'), (new CustomDateTimeImmutable('2020-01-03'))->modify('+1 day')); var_dump($datePeriod->getStartDate()->getFoo()); $strings = []; foreach ($datePeriod as $date) { var_dump($date->getFoo()); } Expected result: ---------------- "foo" "foo" "foo" "foo" Actual result: -------------- NULL NULL NULL NULL -- Edit bug report at https://bugs.php.net/bug.php?id=80047&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=80047&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=80047&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=80047&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=80047&r=needscript Try newer version: https://bugs.php.net/fix.php?id=80047&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=80047&r=support Expected behavior: https://bugs.php.net/fix.php?id=80047&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=80047&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=80047&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=80047&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=80047&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=80047&r=dst IIS Stability: https://bugs.php.net/fix.php?id=80047&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=80047&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=80047&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=80047&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=80047&r=mysqlcfg

« previous php.bugs (#228859) next »