-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement $this return type #7470
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
Thank you so much @nikic for giving this a try. For the record, here is some real word situation where this would be super useful: |
I can see some usages where it makes sense to enforce the implementations return the same object. |
@drealecs I don't think PSR-7 should defined its return type as |
Thank you @nikic and @nicolas-grekas for working on this. We've already achieved a lot with the
Seeing a dollar character in a type declaration also felt a bit odd to me at first. But I must admit that Using |
While in a pure type system this should not exist, it definitely mirrors the reality. I also agree that it should be |
Hi :),
We should not forget
Sawing this, im wondering is reserving edit: i've also shared this snippet with @nicolas-grekas, which is why i think this idea is generally fine (it already exists in practice in PHP, having real language semantics is for the better :)) |
this won't happen. This type is only allowed in return types (just like
I don't understand what you mean here. Typescript uses |
Sorry, i did not saw @stof i simply mean the concept seems legit in a typesystem. For PHP, im wondering if |
This seems like an issue for static analysis and not for the language. On the one hand, this modification introduces a small changes, but on the other hand we introduce unnatural keyword for the PHP language and make solve an issue that can be solved by other ways. It not seems there is an issue that cannot be solved due to lack of |
Forgive my ignorance .. :} but isnt the language already staticly analyzing to a certain degree? To me this is about supporting both flavors, that is |
Since a return type of |
Closing for now as it doesn't seem likely this will go anywhere. |
The What was missing in order to going to vote ? |
The
$this
type is a subtype ofstatic
that only allows returning$this
. On an interface, it enforces a fluent implementation, whilestatic
could also be wither-style API.This has been suggested by @nicolas-grekas, I'm not completely sure it's a good idea yet :)
RFC: https://wiki.php.net/rfc/this_return_type