Re: [RFC][Vote] Property Hooks
| From: | Valentin Udaltsov | Date: | Mon, 10 Jun 2024 12:33:18 +0000 |
| Subject: | Re: [RFC][Vote] Property Hooks | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123565@lists.php.net to get a copy of this message | ||
On Sunday, 9 June, 2024 at 19:03, Nikita Popov <php@npopov.com> wrote:
> On Mon, Apr 15, 2024, at 18:43, Larry Garfield wrote:
>
> The vote for the Property Hooks RFC is now open:
>
> https://wiki.php.net/rfc/property-hooks
>
> Voting will close on Monday 29 April, afternoonish Chicago time.
>
>
> The other thing that stood out to me are the short-hand notations using
> =>. There was a prior RFC on the topic (
> https://wiki.php.net/rfc/short-functions), which has been
> declined. That
> RFC would have introduced => ... as a general shorthand for { return ....; }.
>
> The shorthand notation for get is compatible with that formulation.
> However, the shorthand notation for set is not. In that case => ... isn't
> short for { return ...; }, but rather for { $this->prop = ...; }.
>
> This seems pretty unfortunate to me, and possibly closes the door on
> revisiting a general short function syntax in the future. Mostly I'm
> scratching my head at why this was included in the proposal at all, as I
> would not expect this use of the set hook to be common enough to justify a
> shorthand. The common case is a guard that checks the value without
> modifying it.
>
> Putting this to the "would this shorthand have passed if it were
> introduced by a separate RFC on top of the base implementation" test, I
> think the answer would have been a clear "no".
>
> Regards,
> Nikita
>
Correct me if I'm wrong, but here's how I understand the short-hand
notation in setter hook in combination with
https://wiki.php.net/rfc/short-functions:
```
final class Foo
{
// Before
public string $x {
set {
$this->x = $this->transformX($value);
}
}
private function transformX(string $value): string => trim($value);
// After
public string $x {
set => trim($value);
}
}
```
So it's kinda consistent if you say that only the right side of the
property assignment is replaced with short-hand notation, not the whole
assignment. And then
$this->x = is implicit.
--
Regards,
Valentin