Method parameter promotion support

php.internals

David Rodrigues

5 years ago
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

Pierre

5 years ago
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

Sara Golemon

5 years ago
On Thu, Nov 26, 2020 at 10:43 AM Pierre R. <pierre-php@processus.org> wrote:
> Le 26/11/2020 à 17:30, David Rodrigues a écrit : > > 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; } > > It sounds weird for me,
Same. I'd also add that for solving this problem (boilerplate accessors), I'd much rather see us adopt C# like accessor methods and/or offer something like the expressive methods that Larry suggested a couple weeks ago. Both of those have a more generalized path to addressing this, IMO. -Sara

Nikita Popov

5 years ago
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