Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Conversation

nikic
Copy link
Member

@nikic nikic commented Sep 6, 2021

The $this type is a subtype of static that only allows returning $this. On an interface, it enforces a fluent implementation, while static 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

@nikic nikic added the RFC label Sep 6, 2021
@nicolas-grekas
Copy link
Contributor

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:
symfony/symfony#42714

@drealecs
Copy link

drealecs commented Sep 7, 2021

I can see some usages where it makes sense to enforce the implementations return the same object.
It would be also useful if at some point we would be able to enforce returning a new object, like we need for PSR-7: and we could have something like static & !$this or even static - $this or static ^ $this.
Also, I'm not sure if this should be used instead of $this. But I'll wait for you to open the topic on the mailing list for more feedback.

@stof
Copy link
Contributor

stof commented Sep 7, 2021

@drealecs I don't think PSR-7 should defined its return type as static & !$this. If the request changed is a no-op on the value, it is totally fine to return $this as this does not break immutability.

@derrabus
Copy link
Contributor

derrabus commented Sep 7, 2021

Thank you @nikic and @nicolas-grekas for working on this. We've already achieved a lot with the static return type. I however do see the value in being able to distinguish withers from fluent interfaces. Being able to express this difference through the type system would be good improvement, imho.

Also, I'm not sure if this should be used instead of $this.

Seeing a dollar character in a type declaration also felt a bit odd to me at first. But I must admit that : $this feels intuitive. I know exactly what to expect from this declaration.

Using this instead would have the slight disadvantage that this would need to become a reserved keyword. Currently, it is legal to name a class this: https://3v4l.org/TuCoC

@bwoebi
Copy link
Member

bwoebi commented Sep 7, 2021

While in a pure type system this should not exist, it definitely mirrors the reality.

I also agree that it should be $this as we return a variable $this, not a type - the type of $this is static.

@ro0NL
Copy link

ro0NL commented Sep 7, 2021

Hi :),

But I must admit that : $this feels intuitive.

We should not forget

method($this $param) {}

Sawing this, im wondering is reserving this too late?

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 :))

@stof
Copy link
Contributor

stof commented Sep 7, 2021

We should not forget

method($this $param) {}

this won't happen. This type is only allowed in return types (just like static)

Sawing this, im wondering is reserving this too late?

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 :))

I don't understand what you mean here. Typescript uses this as the type, but JS (and TS) also uses this as the variable name for the instance.

@ro0NL
Copy link

ro0NL commented Sep 7, 2021

Sorry, i did not saw Cannot use $this as param type while scanning. That, eh, makes sense.

@stof i simply mean the concept seems legit in a typesystem. For PHP, im wondering if this would be preferred over $this.

@a-menshchikov
Copy link

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 $this return type (excepts the problems with need of annoying PhpDocs).

@ro0NL
Copy link

ro0NL commented Sep 8, 2021

Forgive my ignorance .. :} but isnt the language already staticly analyzing to a certain degree?

To me this is about supporting both flavors, that is tag(string $name): this vs . tag(string ...$name): void

@dktapps
Copy link
Contributor

dktapps commented Oct 1, 2021

Since a return type of $this has only one possible result, maybe worthwhile having functions with $this return type automatically return $this without a return statement at the end of the function? Would reduce boilerplate for fluent methods ...

@iluuu1994
Copy link
Member

Closing for now as it doesn't seem likely this will go anywhere.

@iluuu1994 iluuu1994 closed this Jun 26, 2022
@VincentLanglet
Copy link
Contributor

The $this return type would be great.
I just saw the RFC https://wiki.php.net/rfc/this_return_type was inactive.

What was missing in order to going to vote ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.