Re: Methods which auto-return the class instance
| From: | Lynn | Date: | Fri, 23 Dec 2022 11:10:59 +0000 |
| Subject: | Re: Methods which auto-return the class instance | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119203@lists.php.net to get a copy of this message | ||
On Fri, Dec 23, 2022 at 10:33 AM Claude Pache <claude.pache@gmail.com>
wrote:
>
> ```php
> public function hit(): $this { $this->counter++; }
> ```
>
> Here, there is only one possible return value, and therefore the
>
return
> instruction might be reasonably omitted.
>
> The real question is: Is it worth to add special cases for some specific
> return values?
>
We do have true, false, and null as return types (
https://www.php.net/releases/8.2/en.php#null_false_true_types).
I'm
personally not opposed to having $this as return type, but it also starts
getting close to short function notations.