php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80047 DatePeriod doesn't support custom DateTimeImmutable
Submitted: 2020-09-03 12:01 UTC Modified: 2022-06-24 10:22 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: oognic at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.2.33 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oognic at gmail dot com
New email:
PHP Version: OS:

 

 [2020-09-03 12:01 UTC] oognic at gmail dot com
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2022-06-24 10:22 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2022-06-24 10:22 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

The fix that I have committed is to *now* allow inherited objects here.

The DatePeriod iterator needs to construct DateTime/DateTimeImmutable objects which can only done by copying internal data, which can't be recreated by calling the constructor of an inherited class. This data needs to be directly injected into a (new) DateTime/DateTimeImmutable object.
 [2022-06-25 16:53 UTC] git@php.net
Automatic comment on behalf of derickr
Revision: https://github.com/php/php-src/commit/973c3f6e241227ffc14c3608c774d7636b798cec
Log: Fixed #80047: DatePeriod doesn't warn with custom DateTimeImmutable
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Jun 27 23:05:45 2022 UTC