php / php/php-src

`DateInterval` no longer respects start/end class types

未关闭
#18,057 10 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@derickr 已经在做这个了。

开始于 2025年3月14日。

Bug Extension: date Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

DateInterval iterated types no longer mirror parameter types
<?php
class TestClass extends DateTimeImmutable {}

$interval = new DatePeriod(
    new TestClass(),
    new DateInterval('P1D'),
    new TestClass('+5 days'),
);

var_dump(
    $interval->start, // TestClass
    $interval->end, // TestClass
);

foreach ($interval as $date) {
    var_dump($date); // TestClass in PHP 7.4-, DateTimeImmutable in PHP 8.0+
}

https://3v4l.org/VIQbo#v7.4.33 // TestClass
https://3v4l.org/VIQbo#v8.0.30 // DateTimeImmutable

This used to be consistent in PHP7.4 and below, start, end AND iterated values were all of the same class,
but since PHP 8.0, they are no longer consistent for some reason, and it's weird, because there should be no issue with compatibility having it the way it used to be. It used to be objectively better.

I only noticed it now because I had to update a legacy project from 7.3 to 8.3. Now I will also have to manually wrap any code that iterates over DatePeriod.

PHP Version

PHP 8.0+

Operating System

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。