After implementing static types in return methods, I noticed one thing that
was illogical, in my opinion.
We have the following code:
interface A
{
public function method1(): static;
}
final class Foo extends B implements A
{
public function method1(): static
{
return $this;
}
}
It seems to me that in final classes, redefining return types in methods
from static to self will not be a violation of covariance, because the
final class will never have children. But now it triggers an error Fatal
error: Declaration of Foo::method1(): Foo must be compatible with
B::method1(): static. What do you think about this?
I think using static inside classes always implies that the class can have
children. And when static occurs in the final class, it's a bit confusing.
So I'd like to see the language provide the ability to replace static with
self, because again, it's not a violation of covariance.
In addition, the same PHPStorm suggests replacing static types with self
types in final classes (if this type is not specified in the prototype).
You can read some conversation and thoughts in github issue (PR is already
attached): https://github.com/php/php-src/issues/17725