rfc:lexical-anon

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:lexical-anon [2016/04/19 13:11] – created krakjoerfc:lexical-anon [2016/04/20 06:44] krakjoe
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: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/lexical-anon   * First Published at: http://wiki.php.net/rfc/lexical-anon
  
 ===== 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, they must be injected via the constructor, which is verbose, repetitive, and widens the margin for error considerably. If the anonymous class has dependencies, they must be injected via the constructor, which is verbose, repetitive, and widens the margin for error considerably.
Line 71: Line 71:
     private $glow;     private $glow;
 };  }; 
 +</code>
 +
 +===== 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.
 +
 +The cognitive overhead of "anything use'd by the declaration is a member property" is almost nil.
 +
 +===== Error Conditions =====
 +
 +The same restrictions that apply to use() on function declarations apply:
 +
 +  * Must not be superglobal name
 +  * Must not be ''$this''
 +
 +The property reference must be well formed, and accessible in the lexical scope:
 +
 +''$object->property''
 +
 +A property name cannot be used twice:
 +
 +''use($prop, $prop)''
 +
 +and
 +
 +''use($prop, $this->prop)''
 +
 +Will both raise compile time errors:
 +
 +<code>
 +Fatal error: Cannot use property name glow twice in /in/file on line 6
 </code> </code>
  
Line 94: Line 127:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-https://github.com/php/php-src/compare/master...krakjoe:lexical-anon+https://github.com/php/php-src/pull/1874
  
 ===== Implementation ===== ===== Implementation =====
rfc/lexical-anon.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1