rfc:constructor_promotion
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:constructor_promotion [2020/04/28 14:17] – Use trailing commas! nikic | rfc:constructor_promotion [2020/05/29 10:59] – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2020-03-26 | * Date: 2020-03-26 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.0 | * Target Version: PHP 8.0 | ||
* Implementation: | * Implementation: | ||
Line 91: | Line 91: | ||
</ | </ | ||
- | It is also not possible to use the '' | + | It is also not possible to use the '' |
<PHP> | <PHP> | ||
Line 160: | Line 160: | ||
While repeating the default value on the property declaration would currently appear harmless, there are forward-compatibility reasons why it is preferable to only specify the default once. | While repeating the default value on the property declaration would currently appear harmless, there are forward-compatibility reasons why it is preferable to only specify the default once. | ||
- | The first is a possible future extension to allow arbitrary | + | The first is a possible future extension to allow arbitrary |
<PHP> | <PHP> | ||
Line 239: | Line 239: | ||
As the example indicates, this allows using doc comment based annotations with promoted properties. | As the example indicates, this allows using doc comment based annotations with promoted properties. | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Most reflection code should not care whether properties are generated or not, but this information will allow reconstructing the structure or the original code more easily. | ||
==== Inheritance ==== | ==== Inheritance ==== | ||
Line 313: | Line 320: | ||
==== Attributes ==== | ==== Attributes ==== | ||
+ | As PHP 8 also introduces [[rfc: | ||
+ | <PHP> | ||
+ | class Test { | ||
+ | public function __construct( | ||
+ | << | ||
+ | public int $prop, | ||
+ | ) {} | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This code could desugar in one of four ways: | ||
+ | |||
+ | - The attribute is applied only to the parameter. | ||
+ | - The attribute is applied only to the implied property. | ||
+ | - The attribute is applied both to the parameter and the property. | ||
+ | - Attributes on promoted properties are forbidden, due to ambiguity. | ||
+ | |||
+ | Here are the possible transformations: | ||
+ | |||
+ | <PHP> | ||
+ | // Option 1: Attribute applies only to parameter. | ||
+ | class Test { | ||
+ | public int $prop; | ||
+ | | ||
+ | public function __construct( | ||
+ | << | ||
+ | int $prop, | ||
+ | ) {} | ||
+ | } | ||
+ | |||
+ | // Option 2: Attribute applies only to property. | ||
+ | class Test { | ||
+ | << | ||
+ | public int $prop; | ||
+ | | ||
+ | public function __construct( | ||
+ | int $prop, | ||
+ | ) {} | ||
+ | } | ||
+ | |||
+ | // Option 3: Attribute applies to both | ||
+ | class Test { | ||
+ | << | ||
+ | public int $prop; | ||
+ | | ||
+ | public function __construct( | ||
+ | << | ||
+ | int $prop, | ||
+ | ) {} | ||
+ | } | ||
+ | |||
+ | // Option 4: Error, cannot use attributes with constructor parameter promotion. | ||
+ | </ | ||
+ | |||
+ | This RFC proposes to use option 3 (applying the attribute to both property and parameter), as it is the most flexible. The '' | ||
+ | |||
+ | However, I consider this to be something of an implementation detail. If further work on attributes prior to the PHP 8 release shows that it would be advantageous to place the attribute only on the property, we should be open to such a change. | ||
==== Coding Style Considerations ==== | ==== Coding Style Considerations ==== | ||
Line 402: | Line 466: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Yes/No. | + | Voting started 2020-05-15 and closes 2020-05-29. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ | ||
rfc/constructor_promotion.txt · Last modified: 2020/08/01 23:38 by carusogabriel