-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added missing obj methods to DateTimeInterface #13491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are vague plans to deprecate and remove DateTime
, in which case the interface should be deprecated if we want to keep disallowing userland implementations, or otherwise I think the mutator methods could be added to the interface after the interface assumes/requires an immutable implementation.
@@ -316,12 +316,43 @@ interface DateTimeInterface | |||
/** @tentative-return-type */ | |||
public function format(string $format): string; | |||
|
|||
/** @tentative-return-type */ | |||
public function modify(string $modifier): DateTimeInterface|false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO adding any mutator methods to the interface is not applicable now, because the two implementations have different behavior regarding mutability. In practice, this means that users of the DateTimeInterface
should deal with the possibility that the interface implementation is either mutable or immutable.
function setTime(DateTimeInterface $d) {
$d1 = clone $d; // cloning is needed as a defensive measure
$d1->modify("+1 days");
return $d1;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the behavior is different, but this kind of difference is not possible to be defined in method signatures.
Actually your example should be
function setTime(DateTimeInterface $d) {
$new = clone $d;
$new = $new->modify("+1 days");
return $new;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree mutator methods should not be added. When one works with an interface instance it's impossible to know whether it's a mutable or immutable instance. If PHPStan, IDE etc. need some more information then we can write stub files for them. I would leave it as is (hoping this interface will be removed together with mutable class).
/** @tentative-return-type */ | ||
public function getOffset(): int; | ||
|
||
/** @tentative-return-type */ | ||
public function getMicroseconds(): int; // TODO: Rename https://github.com/php/php-src/pull/13486 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one method is definitely missing from the interface right now, so maybe this one could be committed separately without any further discussion.
@kocsismate I totally agree, that the mutable To go forward I would suggest the following ...
function nextDay(DateTime|DateTimeImmutable $d) {
$new = clone $d;
$new = $new->modify("+1 days");
return $new;
} What do you think ? |
@kocsismate can you elaborate? Are there any plans more recent than Derick's tweet from 1.5 year ago https://twitter.com/derickr/status/1551611856007069696?lang=en? |
@javaDeveloperKid No, I am not aware of any specifics. |
@marc-mabe Yes, I agree with the plan, thank you for the improvements! I guess the removal of the magic methods from the interface would only affect people who call these methods directly for some weird reason (maybe some static analyisis issues could pop up) ^^ So I think the change is fine. But let's wait for Derick's opinion, since he was the one who added them to the interface. |
I think there is nothing to do here now? The getMicroseconds was added I believe. And I don't think adding the mutator methods is a good idea, as they don't have the same behaviour (new object vs internally modified one). |
Hi @derickr , |
I am going to close this, until we have a new full thought-through API. |
DateTimeInterface
can not be implemented by user land which limits the interface toDateTime
andDateTimeImmutable
but it's missing a lot of object methods available in both
DateTime
andDateTimeImmutable
.This makes your static analyzers (IDE, PHPStan and friends, ...) know which methods to expect.
Question: The interface already has
__wakeup()
,__serialize()
,__unserialize()
but not__set_state()
which I personally would not expect in an interface if there isb't a very good reason ...__set_state()
be added as well?NOTE: This somehow depends on #13486 or vise versa.