php / php/php-src

`DateInterval` no longer respects start/end class types

Đang mở
#18,057 10 bình luận 0 reaction 1 người được giao Xem trên GitHub

@derickr đang làm issue này rồi.

Từ ngày 14/3/2025.

Bug Extension: date Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.