php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78401 DateTime::modify signature change
Submitted: 2019-08-11 20:06 UTC Modified: 2019-08-12 12:27 UTC
From: kylekatarnls at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Compile Failure
PHP Version: Next Major Version OS: Linux
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: kylekatarnls at gmail dot com
New email:
PHP Version: OS:

 

 [2019-08-11 20:06 UTC] kylekatarnls at gmail dot com
Description:
------------
My last Travis-CI test failed (while same code worked until there) for PHP 8.0.0-dev with the following message:

Declaration of DateTime::modify(string $modify) must be compatible with I::modify($modify)

Test script:
---------------
interface I extends DateTimeInterface
{
  modify($modify);
}

class A extends DateTime implements I
{
  modify($modify) {}
}

Expected result:
----------------
I would expect my interface to be compatible with DateTime since it's covariant. I accept more types but string is included so it should pass as there is no reason to forbid this extension.

Then I would be compatible with both PHP 7 and 8 but if I change the signature to a the string typing, it will fail on PHP 7.

Actual result:
--------------
Declaration of DateTime::modify(string $modify) must be compatible with I::modify($modify)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-11 20:42 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2019-08-11 20:42 UTC] nikic@php.net
I can't reproduce this.
 [2019-08-12 06:21 UTC] kylekatarnls at gmail dot com
-Status: Feedback +Status: Closed
 [2019-08-12 06:21 UTC] kylekatarnls at gmail dot com
Hi, sorry, my mistake, the method was not implemented. In fact I relied on the fact that both DateTime and DateTimeImmutable have the modify() method, but as it's not declared in DateTimeInterface (I don't know why, it would have seem logical), I had to declare it in my own interface.

Now due to this change in PHP 8, I have to add

```
public function modify($modify)
{
    return parent::modify((string) $modify);
}
```

So question for curiosity. Why DateTimeInterface does not declare all common methods of DateTime and DateTimeImmutable.
 [2019-08-12 11:55 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2019-08-12 11:55 UTC] nikic@php.net
While both DateTime and DateTimeImmutable have a modify() method, the behavior is not compatible, so it's not part of the interface. If you accept a generic DateTimeInterface parameter, it's generally only safe to call the read-only methods on it, not the mutating methods, as these may exhibit differing behavior.
 [2019-08-12 12:27 UTC] kylekatarnls at gmail dot com
Hi, I understand the point of view.

Now I face an issue with a missing parameter in __set_state for DateTimeImmutable:
https://github.com/php/php-src/pull/4526

Is the PR enough or should I open a bug?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 03:01:28 2024 UTC