You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Override static in return types with self in final classes #17725
After implementing static types in return methods, I noticed one thing that was illogical, in my opinion.
We have the following code:
<?phpdeclare(strict_types=1);
interface A
{
publicfunctionmethod1(): static;
}
abstractclass B
{
abstractpublicfunctionmethod2(): static;
}
trait C
{
abstractpublicfunctionmethod3(): static;
}
finalclass Foo extends B implements A
{
use C;
publicfunctionmethod1(): static
{
return$this;
}
publicfunctionmethod2(): static
{
return$this;
}
publicfunctionmethod3(): static
{
return$this;
}
}
$foo = newFoo();
var_dump($foo->method1());
var_dump($foo->method2());
var_dump($foo->method3());
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::method2(): Foo must be compatible with B::method2(): static. What do you think about this?
finalclass Foo extends B implements A
{
use C;
publicfunctionmethod1(): self
{
return$this;
}
publicfunctionmethod2(): self
{
return$this;
}
publicfunctionmethod3(): self
{
return$this;
}
}
In addition, the same PHPStorm suggests replacing static types with self types in final classes (if this type is not specified in the prototype).
Below I attach a link to the pull request in which I implemented this logic.
I cannot see anything wrong with this transformation either.
static ⊑ self where static is dependent on the class calling it. If self is final, then the class calling the method is always self as such static = self.
The one advantage I could see is for the engine to do this transformation, which could simplify some variance checks and give more robust Reflection information if/when we add isSubTypeOf style methods to reflection if done in tandem with #17755 / #11460
I added union types support for this feature and wrote additional tests for it. But I don't understand how to add support for this with intersection types) So for now I just added the !intersection check :)
It's sound, but is it necessary? If the child removes final, it will need to migrate back to static. But I don't object.
Yes, there may be some inconvenience with this. But here, in fact, we are talking only about the fact that the language has the ability to replace static with self. This should not be mandatory. For me personally, it would be convenient. I am used to not using the static keyword inside when writing final classes, because it is a little confusing.
It's sound, but is it necessary? If the child removes final, it will need to migrate back to static. But I don't object.
It seems to me that 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.
Activity
php#17725 add more tests
Girgias commentedon Feb 7, 2025
I cannot see anything wrong with this transformation either.
static ⊑ selfwherestaticis dependent on the class calling it. Ifselfis final, then the class calling the method is alwaysselfas suchstatic = self.iluuu1994 commentedon Feb 8, 2025
It's sound, but is it necessary? If the child removes
final, it will need to migrate back tostatic. But I don't object.Girgias commentedon Feb 10, 2025
The one advantage I could see is for the engine to do this transformation, which could simplify some variance checks and give more robust Reflection information if/when we add
isSubTypeOfstyle methods to reflection if done in tandem with #17755 / #11460iluuu1994 commentedon Feb 10, 2025
Letting the engine do it would be fine, although it is obviously observable. Might be worth sending a quick e-mail to the list.
php#17725 change way to checking availability to replace static with …
rekmixa commentedon Feb 10, 2025
I added union types support for this feature and wrote additional tests for it. But I don't understand how to add support for this with intersection types) So for now I just added the !intersection check :)
https://github.com/php/php-src/pull/17724/files#diff-16d00017a3ed574cca152e8754246a1424db74e221934eb1c0c2513442882615R575
Maybe someone else can do this?
rekmixa commentedon Feb 10, 2025
Yes, there may be some inconvenience with this. But here, in fact, we are talking only about the fact that the language has the ability to replace static with self. This should not be mandatory. For me personally, it would be convenient. I am used to not using the static keyword inside when writing final classes, because it is a little confusing.
rekmixa commentedon Feb 10, 2025
It seems to me that 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.
rekmixa commentedon Feb 10, 2025
Okay, i'll send
rekmixa commentedon Feb 10, 2025
https://externals.io/message/126367
php#17725 add more tests for union types
php#17725 fix checking inheritance while replacing static with self i…
4 remaining items
php#17725 add brackets
php#17725 remove unnecessary tests & change test descriptions & move …
php#17725 change tests & change comparing instance of while checking …
php#17725 fix test override_static_type_with_self_in_final_class_with…
php#17725 rollback moving ZEND_TYPE_FULL_MASK to variable
php#17725 remove redundand checking for is_intersection & move checki…
php#17725 change checking ability to replace static with self & updat…
php#17725 remove unlinked_instanceof
php#17725 update comment
php#17725 remove redundant whitespace change
Allow substituting static for self in final classes
php/php-src#17725 add info about substituting static with self in Cha…
Merge pull request #1649 from rekmixa/add-issue-19722-to-changelog-8.5