Req #78401 [Csd]: DateTime::modify signature change
| From: | nikic@php.net | Date: | Mon, 12 Aug 2019 11:55:57 +0000 |
| Subject: | Req #78401 [Csd]: DateTime::modify signature change | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222192@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78401&edit=1
ID: 78401
Updated by: nikic@php.net
Reported by: kylekatarnls at gmail dot com
Summary: DateTime::modify signature change
Status: Closed
Type: Feature/Change Request
Package: Compile Failure
Operating System: Linux
PHP Version: Next Major Version
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2019-08-12 06:21:32] 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-11 20:42:46] nikic@php.net
I can't reproduce this.
------------------------------------------------------------------------
[2019-08-11 20:06:30] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78401&edit=1