rfc:property_accessors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:property_accessors [2021/05/04 09:25] – nikic | rfc:property_accessors [2022/04/28 09:30] (current) – Fix typos ilutov | ||
---|---|---|---|
Line 363: | Line 363: | ||
</ | </ | ||
- | Specifying the same accessor multiple | + | Specifying the same accessor multiple |
=== By-reference getter === | === By-reference getter === | ||
Line 387: | Line 387: | ||
</ | </ | ||
- | These indirect | + | These indirect |
However, indirect array modification, | However, indirect array modification, | ||
Line 420: | Line 420: | ||
While nominally well-defined, | While nominally well-defined, | ||
+ | |||
+ | TODO: An open problem is how implicit by-value '' | ||
+ | |||
+ | <PHP> | ||
+ | class Test { | ||
+ | public $prop = null { get; private set; } | ||
+ | } | ||
+ | |||
+ | $test = new Test; | ||
+ | foreach ($test as &$prop) { | ||
+ | $prop = 1; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This needs to be prevented in some way. This could either error, or it could silently return a value for properties for which no reference can be acquired. Otherwise it would no longer be possible to iterate arbitrary objects by-reference. | ||
=== Isset and unset === | === Isset and unset === | ||
Line 499: | Line 514: | ||
=== Interaction with by-reference getter === | === Interaction with by-reference getter === | ||
- | It is worth pointing out that the combination of ''& | + | It is worth pointing out that the combination of ''& |
<PHP> | <PHP> | ||
Line 524: | Line 539: | ||
class A { | class A { | ||
public $prop { | public $prop { | ||
- | get { echo __METHOD, " | + | get { echo __METHOD__, " |
- | set { echo __METHOD, " | + | set { echo __METHOD__, " |
} | } | ||
} | } | ||
class B extends A { | class B extends A { | ||
public $prop { | public $prop { | ||
- | set { echo __METHOD, " | + | set { echo __METHOD__, " |
} | } | ||
} | } | ||
Line 550: | Line 565: | ||
public int|string $covariant { get; } | public int|string $covariant { get; } | ||
// This property is useless, but will serve for the sake of illustration. | // This property is useless, but will serve for the sake of illustration. | ||
- | public int|string $contravariant { set {} } | + | public int|string $contravariant { set { /* ... */ } } |
} | } | ||
Line 905: | Line 920: | ||
class Test { | class Test { | ||
// Illegal: Implicit get, explicit set. | // Illegal: Implicit get, explicit set. | ||
- | public string $prop { get; set {} } | + | public string $prop { |
+ | | ||
+ | | ||
+ | | ||
// Illegal: Implicit set, explicit get. | // Illegal: Implicit set, explicit get. | ||
- | public string $prop { get {} set; } | + | public string $prop { |
+ | | ||
+ | | ||
+ | | ||
} | } | ||
</ | </ | ||
Line 930: | Line 951: | ||
// Illegal: Default value on property with explicit accessors. | // Illegal: Default value on property with explicit accessors. | ||
public $prop = "" | public $prop = "" | ||
- | get {} | + | get { /* ... */ } |
} | } | ||
} | } | ||
Line 963: | Line 984: | ||
This limitation exists to prevent embedding of very large property declarations in the constructor signature. | This limitation exists to prevent embedding of very large property declarations in the constructor signature. | ||
- | === var_dump, get_object_vars() etc === | + | === var_dump(), array cast, foreach |
- | '' | + | '' |
<PHP> | <PHP> | ||
Line 979: | Line 1000: | ||
// | // | ||
// } | // } | ||
+ | |||
+ | var_dump((array) $test); | ||
+ | // array(1) { | ||
+ | // | ||
+ | // | ||
+ | // } | ||
+ | |||
+ | foreach ($test as $name => $value) { | ||
+ | echo " | ||
+ | } | ||
+ | // prop1: 42 | ||
</ | </ | ||
rfc/property_accessors.1620120321.txt.gz · Last modified: 2021/05/04 09:25 by nikic