rfc:property-capture
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:property-capture [2023/04/15 20:28] – created imsop | rfc:property-capture [2023/04/15 20:51] – imsop | ||
---|---|---|---|
Line 33: | Line 33: | ||
$bar = 2; | $bar = 2; | ||
$anon = new class($foo, $bar) { | $anon = new class($foo, $bar) { | ||
- | | + | |
- | | + | |
+ | | ||
public function __construct($foo, | public function __construct($foo, | ||
$this-> | $this-> | ||
Line 41: | Line 42: | ||
}; | }; | ||
</ | </ | ||
- | |||
- | ==== Visibility Modifiers ==== | ||
==== Renaming ==== | ==== Renaming ==== | ||
+ | |||
+ | By default, the property takes the same name as the outer variable, but this can be over-ridden using the syntax '' | ||
+ | |||
+ | <code php> | ||
+ | $foo = 1; | ||
+ | $bar = 2; | ||
+ | $anon = new class use ($foo as $one, $bar as $two, $bar as $three) {}; | ||
+ | </ | ||
+ | |||
+ | Is equivalent to: | ||
+ | |||
+ | <code php> | ||
+ | $foo = 1; | ||
+ | $bar = 2; | ||
+ | $anon = new class($foo, $bar, $bar) { | ||
+ | var $one; | ||
+ | var $two; | ||
+ | vat $three; | ||
+ | | ||
+ | public function __construct($one, | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | ==== Modifiers and Type ==== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The modifiers allowed are the same as for Constructor Property Promotion, which is used internally to declare the properties; that is currently: | ||
+ | |||
+ | * One of '' | ||
+ | * Optional '' | ||
+ | * A type specification | ||
+ | |||
+ | For example: | ||
+ | |||
+ | <code php> | ||
+ | $foo = 1; | ||
+ | $bar = 2; | ||
+ | $anon = new class use ($foo as private, $bar as protected readonly int, $bar as ?int $alsoBar) {}; | ||
+ | </ | ||
+ | |||
+ | Is equivalent to: | ||
+ | |||
+ | <code php> | ||
+ | $foo = 1; | ||
+ | $bar = 2; | ||
+ | $anon = new class($foo, $bar, $bar) { | ||
+ | private $foo; | ||
+ | protected readonly int $bar; | ||
+ | var ?int $alsoBar; | ||
+ | | ||
+ | public function __construct($foo, | ||
+ | $this-> | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
===== Restrictions and Errors ===== | ===== Restrictions and Errors ===== |
rfc/property-capture.txt · Last modified: 2023/04/23 21:15 by imsop