Re: Method parameter promotion support
| From: | Pierre R. | Date: | Thu, 26 Nov 2020 16:42:53 +0000 |
| Subject: | Re: Method parameter promotion support | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112300@lists.php.net to get a copy of this message | ||
Le 26/11/2020 à 17:30, David Rodrigues a écrit :
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. Atenciosamente, David Rodrigues It sounds weird for me, I can see two naive questions:- what will happen if you don't call the method, will the property still exists on the object ? - what will happen if you define it on more than one method ? And one more serious regarding code readability and maintainability: for me, class constructors and classic class property definition acts together as an index, that I can read to know what's inside a type structure ; if you start to explode that everywhere in arbitrary places code and I have to read and understand in order to use it, I will get angry very quickly, If I can't find property definitions neither in their rightful place, nor in the constructor. If you code looks like a puzzle that you have to solve in order to understand what it does, it's probably not that good. I don't like this idea. -- Pierre