Re: Add $this return type

From: Date: Wed, 26 Jun 2024 19:16:22 +0000
Subject: Re: Add $this return type
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-123881@lists.php.net to get a copy of this message
Juliette Reinders Folmer said: > Not a fan of using a variable as a return type. This will cause > havoc for Tokenizer based static analysis tooling. I will concede that until the tools are updated to handle the new (currently invalid) syntax, it would cause issues. But that happens everytime a new syntax is introduced into PHP anyway (the original return-types RFC, match, the proposed property accessors, etc.), so I'm a bit confused as why this is an issue that would stop the RFC? --- Juliette Reinders Folmer said: > Also don't really see the need as there is the static > return type > already. To quote Nicolas, who said it far better than I could: > I tried switching from the @return $this annotation to > function (): static, which is the closest approximation > currently > supported by the engine, when I realized that this would loosen the > semantics of the corresponding interfaces[.] --- Marco Pivetta said: > I'd see it as useful if the type conveyed identity too, although it > means that I would never use it, since I design immutable APIs, when > possible :D If reading documentation, I'd argue that seeing function(): $this in a class already conveys plenty of "identity". If you're talking about using the Reflection API to get identity the original RFC by Nikita stated: > The $this type is a subtype of > static. As such, it is possible > to restrict a static type to > $this in a subclass (but not the > other way around). > In reflection, the $this type is represented as a > ReflectionNamedType with isBuiltin() == > true and > getName() == "$this". So perhaps a change is needed in the returned ReflectionNamedType to better identity the actual class of $this? Or am I missing your point about identity here? --- Bruce Weirdan said: >> Luigi Cardamone wrote: >> Is it possible to replace "$this" with "this"? Cleaner and coherent >> with "self". > > That wouldn't be possible, as this is a valid class name: The "Return Type Declarations", "Scalar Type Declarations", "Reserve More Types", and "Void Return Type" RFCs all prohibited their usage as class, interface and trait names. Using this or same or whatever else is decided is just par for the course. However, that said, a search of github for: - language:php "class This " returns 5.1k results (but it appears that most results are docblock comments) - language:php "class This extends" returns 27 results - language:php "class This implements" returns 8 results - language:php "class Same " returns 290 results - language:php "class Same extends" returns 193 results - language:php "class Same implements" returns 14 results - language:php "@return $this" returns 485k results. --- Regards, radar3301

« previous php.internals (#123881) next »