rfc:lexical-anon
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:lexical-anon [2016/04/19 13:11] – created krakjoe | rfc:lexical-anon [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Lexical Scope Support for Anonymous Classes ====== | ====== PHP RFC: Lexical Scope Support for Anonymous Classes ====== | ||
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2016-04-19 | * Date: 2016-04-19 | ||
* Author: krakjoe | * Author: krakjoe | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Anonymous classes are cumbersome to use because they lack support for lexical scope variables. | + | Anonymous classes are cumbersome to use because they lack support for lexical scope [use()]. |
If the anonymous class has dependencies, | If the anonymous class has dependencies, | ||
Line 71: | Line 71: | ||
private $glow; | private $glow; | ||
}; | }; | ||
+ | </ | ||
+ | |||
+ | ===== Syntax Choices ===== | ||
+ | |||
+ | Placing the [use(...)] anywhere else in the class declaration seems to be confusing, the current position seems the obvious choice. | ||
+ | |||
+ | It has been suggested that it would be better to have [use(...)] on each function declaration. This would lead to a lot of repetition, and fragmented code that is more difficult to reason about. | ||
+ | |||
+ | Another suggestion made is to allow symbols inline, so that the following code is legal: | ||
+ | |||
+ | <code php> | ||
+ | class { | ||
+ | public $prop = $var; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This has some appealing advantages, such as using expression to initialize properties. But it also raises huge inconsistencies, | ||
+ | |||
+ | <code php> | ||
+ | class { | ||
+ | public $prop = new Foo(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Be allowed, while not allowing the same thing in normal classes. | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | <code php> | ||
+ | class { | ||
+ | public $prop = & | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This is almost literally backwards. | ||
+ | |||
+ | If we are going to allow expressions for anonymous classes that we do not allow for normal classes, then it is better to leave that for another RFC. | ||
+ | |||
+ | What we are doing is only importing symbols, we don't //need// to invent a new way to initialize them. | ||
+ | |||
+ | The cognitive overhead of " | ||
+ | |||
+ | ===== Error Conditions ===== | ||
+ | |||
+ | The same restrictions that apply to use() on function declarations apply: | ||
+ | |||
+ | * Must not be superglobal name | ||
+ | * Must not be '' | ||
+ | |||
+ | The property reference must be well formed, and accessible in the lexical scope: | ||
+ | |||
+ | <code php> | ||
+ | $object-> | ||
+ | </ | ||
+ | |||
+ | A property name cannot be used twice: | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | and | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | Will both raise compile time errors: | ||
+ | |||
+ | < | ||
+ | Fatal error: Cannot use property name glow twice in /in/file on line 6 | ||
</ | </ | ||
Line 94: | Line 161: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | https:// | + | https:// |
===== Implementation ===== | ===== Implementation ===== |
rfc/lexical-anon.1461071481.txt.gz · Last modified: 2017/09/22 13:28 (external edit)