PHP8 "static" return type
Hi! I just upgraded psr/http-message to ^2.0 and my IDE started yelling at me because my library already requires a minimum of PHP 8.1 and i was already using the new static return type.
src\Psr7\Message.php:45 PhanParamSignatureRealMismatchReturnType Declaration of function withProtocolVersion(string $version) : static should be compatible with function withProtocolVersion(string $version) : \Psr\Http\Message\MessageInterface (method where the return type in the real signature is 'static' cannot override method where the return type in the real signature is '\Psr\Http\Message\MessageInterface') defined in vendor\psr\http-message\src\MessageInterface.php:41
Here, I'm proposing a v3.0 that requires a minimum of PHP 8.0:
- return types have changed from <interface> to
static -
mixedtypes have been added - several union types (such as
array|string) have been added - additionally I've fixed some grammar and typos in a different branch - you can have these too as separate PR
This update would make the PSR-7 interfaces finally fully typed and I am asking (no I am begging) for a swift review and release ideally before PHP 9 arrives. Thanks!
Edit: I am very well aware that this would break compatibility with v2.0. However, the issue is not the BC break of this PR but the fact that v2.0 was released 3 months ago (2023-04-17) and PHP 8.0 was released almost 3 years ago (2020-11-06), so missing static was not just an oversight, but a collective failure.
So how to proceed? The easiest "fix" for damage control would be to just remove the <interface> return types from the current v2.0 and re-tag it, so that future installs just ignore the return type while maintaining compatibility in both directions. As an alternative, limit v2,0 to PHP ^7.2.
Declaring a return type of static does not throw any sort of mismatch error in any version of php 8.x
Sounds like Phan was raising a false positive PhanParamSignatureRealMismatchReturnType error
(fixed? I didn't see this specific bug under bug reports... just an issue with interfaces declaring a return type of self... which was a early php 7.x bug)
// no error... something something covariance https://www.php.net/manual/en/language.oop5.variance.php
// returning static satisfies the contract with interface
public function withProtocolVersion(string $version): static
{
if ($version === $this->protocolVersion) {
return $this;
}
$clone = clone $this;
$clone->protocolVersion = $version;
return $clone;
}
Unrelated: what was the point of the 1.1 release?
Sounds like Phan was raising a false positive PhanParamSignatureRealMismatchReturnType error
It might have been and is perhaps fixed in the meantime. However, the issue with incompatible return types in child classes still stands, which is what static is supposed to solve. The linked RFC explains in detail why.