Re: Method parameter promotion support
| From: | Nikita Popov | Date: | Mon, 30 Nov 2020 15:13:34 +0000 |
| Subject: | Re: Method parameter promotion support | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112319@lists.php.net to get a copy of this message | ||
On Thu, Nov 26, 2020 at 5:31 PM David Rodrigues <david.proweb@gmail.com>
wrote:
> Hello!
>
> It is just an idea to discuss. PHP 8 now supports parameter promotion via
> constructor. Could this idea also work for common methods?
>
> public function setAge(private int $age) {}
> ===
> private int $age;
> public function setAge(int $age) { $this->age = $age; }
>
> The only problem I can see would be the difficulty of identification of the
> possible properties, since it would be necessary to check all method
> signatures to know about all possible properties.
>
> In this case, perhaps, it would be enough to allow the promotion only if
> the property already exists.
>
> private int $age; // Required.
> public function setAge(private int $age) {}
>
> Just to think about whether it is possible to think of something along
> these lines, but I'm still not sure if it's a good option.
>
Same as the others, this doesn't make much sense to me. The "private int
$age" syntax is supposed to combine a property declaration and a property
assignment, while here you seem to only want half of that. I think if we
really want to have a short-hand syntax for this, it should be:
public function setAge(int $this->age) {}
However, I don't think this is actually worthwhile. Since the introduction
of typed properties in PHP 7.4, I would consider getters/setters to be an
anti-pattern in nearly all cases, and a potential future addition of
accessors would obviate the need for them entirely.
Regards,
Nikita