Re: [RFC] Property hooks, nee accessors
| From: | Lynn | Date: | Mon, 15 May 2023 08:32:48 +0000 |
| Subject: | Re: [RFC] Property hooks, nee accessors | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120280@lists.php.net to get a copy of this message | ||
On Mon, May 15, 2023 at 1:41 AM Hendra Gunawan <the.liquid.metal@gmail.com>
wrote:
> 2. The shorthand notations supported (the shortest one) creates
> impaired syntax, and not pretty to look at for constructor property
> promotion.
>
It's starting to get crowded in object constructors. The following example
is so much more readable and maintainable imo. Would it be possible to
still add the quick assignment as a language feature? I'm personally not
happy having property definitions in both the class body _and_ the
constructor signature.
```php
class User {
private string $first;
private string $last;
public string $fullName {
get => $this->first . ' ' . $this->last;
set => [$this->first, $this->last] = explode(' ', $value, 2);
}
public function __construct($this->fullName) {}
}
// vs
class User {
private string $first;
private string $last;
public function __construct(
public string $fullName {
get => $this->first . ' ' . $this->last;
set => [$this->first, $this->last] = explode(' ', $value, 2);
}
) {}
}
// or
class User {
private string $first;
private string $last;
public string $fullName {
get => $this->first . ' ' . $this->last;
set => [$this->first, $this->last] = explode(' ', $value, 2);
}
public function __construct(string $fullName)
{
$this->fullName = $fullName;
}
}
```