Skip to content

PHP 8.3 / 8.4: DatePeriod::__construct() replacement/deprecation #1977

@afilina

Description

@afilina

Related to #1589 and #1731

Analysis

Recap of existing PHP 8.2 behavior based on the RFC:

class DatePeriod
{
    // Signature 1: can be called with 3 or 4 arguments.
    public function __construct(DateTimeInterface $start, DateInterval $interval, int $recurrences, int $options = 0) {}
 
    // Signature 2: can be called with 3 or 4 arguments.
    public function __construct(DateTimeInterface $start, DateInterval $interval, DateTimeInterface $end, int $options = 0) {}
 
    // Signature 3: can be called with 1 or 2 arguments.
    public function __construct(string $isostr, int $options = 0) {}
}

Updated behavior for each version:

class DatePeriod
{
    // Signature 1: unchanged behavior, signature 4 will merely merge 1 and 2.
    
    // Signature 2: unchanged behavior, signature 4 will merely merge 1 and 2.

    // Signature 3: deprecated in 8.4, removed in a future (TBD) major version.

    // Signature 4: unchanged behavior, merges signatures 1 and 2 in a future (TBD) major version.
    // From the user's perspective, this is only a documentation update.
    public function __construct(DateTimeInterface $start, DateInterval $interval, DateTimeInterface|int $end, int $options = 0) {} 
 
    // Named constructor: replaces signature 3, added in PHP 8.3
    public static function createFromISO8601String(string $specification, int $options = 0): static {}
}

Detection in PHP 8.2

  • DatePeriod::createFromISO8601String is invalid.
  • Any form of new DatePeriod(...) is unchanged and out of scope for this version, see "Syntax Variations" below.

Detection in PHP 8.3

  • DatePeriod::createFromISO8601String is valid.
  • Any form of new DatePeriod(...) is unchanged and out of scope for this version, see "Syntax Variations" below.

Detection in PHP 8.4

  • new DatePeriod(...) with 1 or 2 arguments is deprecated, add warning. See "Syntax Variations" below.

Syntax Variations

  • new DatePeriod(...) ✅ Can be handled by PHPCompatibility.
  • $className = 'DatePeriod'; new $className(); ❌ Unreliable, as $className could be defined anywhere.
  • new SomeClassWhichExtendsDatePeriod(...) ❌ Can't be handled.
  • class Foo extends DatePeriod { ... parent::__construct(...) ... } ✅ Should be doable.

3v4l.org

References

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions