rfc:propertygetsetsyntax-alternative-typehinting-syntax
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:propertygetsetsyntax-alternative-typehinting-syntax [2013/01/04 14:16] – created nikic | rfc:propertygetsetsyntax-alternative-typehinting-syntax [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2013-01-04 | * Date: 2013-01-04 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
===== Introduction ===== | ===== Introduction ===== | ||
Line 84: | Line 84: | ||
<code php> | <code php> | ||
public DateTime $date; | public DateTime $date; | ||
+ | </ | ||
+ | |||
+ | ===== Default values and nullability ===== | ||
+ | |||
+ | When the shorthand '' | ||
+ | |||
+ | <code php> | ||
+ | public array $foo = [1, 2, 3]; | ||
+ | </ | ||
+ | |||
+ | The nullability semantics are also the same as with parameter typehints: | ||
+ | |||
+ | <code php> | ||
+ | public DateTime $date; | ||
+ | public DateTime $date = null; // can assign null | ||
+ | </ | ||
+ | |||
+ | Note that properties are '' | ||
+ | |||
+ | Default values can only be used on the shorthand notation. If accessor methods are specified a default value can not be used. The only exception is the '' | ||
+ | |||
+ | <code php> | ||
+ | public DateTime $date = null { | ||
+ | get { ... } set { ... } | ||
+ | } | ||
</ | </ | ||
===== Benefits of the proposed syntax ===== | ===== Benefits of the proposed syntax ===== | ||
- | The proposed syntax has several benefits over the current syntax, with the main one being that it **optimizes a very common use case** (while not making other use cases worse). As mentioned above about 15% of all accessor methods in Symfony would be able to use the '' | + | The proposed syntax has several benefits over the current syntax, with the main one being that it **optimizes a very common use case** (while not making other use cases worse). As mentioned above about 15% of all accessor methods in Symfony would be able to use the '' |
Even when the shorthand can not be used (because one actually defines some accessors) I would argue that the proposed syntax is more elegant than the current one, mainly because the **relevant API information is focused** at one point at the start of the declaration. For interacting with the class only the visibility and the typehint are of importance, whereas the actual implementation is irrelevant. The '' | Even when the shorthand can not be used (because one actually defines some accessors) I would argue that the proposed syntax is more elegant than the current one, mainly because the **relevant API information is focused** at one point at the start of the declaration. For interacting with the class only the visibility and the typehint are of importance, whereas the actual implementation is irrelevant. The '' | ||
Line 132: | Line 157: | ||
===== Patch ===== | ===== Patch ===== | ||
- | I haven' | + | The patch for this proposal is available here: https:// |
+ | |||
+ | ===== Voting ===== | ||
+ | |||
+ | This proposal depends on the main accessors RFC. The result | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | The vote ended 3 in favor, 12 against, as such this feature is declined. |
rfc/propertygetsetsyntax-alternative-typehinting-syntax.1357309018.txt.gz · Last modified: 2017/09/22 13:28 (external edit)