rfc:namespaced_names_as_token

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:namespaced_names_as_token [2020/06/15 15:16] – created nikicrfc:namespaced_names_as_token [2020/06/15 17:05] nikic
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-PHP currently treats namespaced names like ''Foo\Bar'' as a sequence of identifier and namespace separator tokens. This RFC proposed to treat namespaced names as a single token, an as such allow reserved keywords to appear inside them. At the same time, it proposes to lift reserved keyword restrictions for class, function and constant declarations.+PHP currently treats namespaced names like ''Foo\Bar'' as a sequence of identifier and namespace separator tokens. This RFC proposed to treat namespaced names as a single token, and as such allow reserved keywords to appear inside them. At the same time, it proposes to lift reserved keyword restrictions for class, function and constant declarations.
  
 The motivation is to reduce the backwards compatibility impact of new reserved keyword additions in future versions of PHP. To give a specific example, PHP 7.4 added the ''fn'' keyword as part of arrow function support. This broke my [[https://github.com/nikic/iter|iter library]], because it was using ''fn'' as part of a namespace name. However, this breakage was entirely unnecessary! Here is a typical usage example: The motivation is to reduce the backwards compatibility impact of new reserved keyword additions in future versions of PHP. To give a specific example, PHP 7.4 added the ''fn'' keyword as part of arrow function support. This broke my [[https://github.com/nikic/iter|iter library]], because it was using ''fn'' as part of a namespace name. However, this breakage was entirely unnecessary! Here is a typical usage example:
Line 48: Line 48:
  
 ===== Proposal ===== ===== Proposal =====
 +
 +==== Changes to namespaced names ====
  
 PHP distinguishes four kinds of namespaced names: PHP distinguishes four kinds of namespaced names:
Line 83: Line 85:
  
 <PHP> <PHP>
-// This is interpreted as T_LIST and cannot be used as an identifier:+// This is interpreted as T_LIST (i.e., as a reserved keyword):
 List List
 // All of the following are interpreted as legal namespaced names: // All of the following are interpreted as legal namespaced names:
Line 107: Line 109:
 // This would have previously been interpreted as $foo = Foo\call($bar), // This would have previously been interpreted as $foo = Foo\call($bar),
 // now it will result in a parser error. // now it will result in a parser error.
-$foo = Foo+$foo = Foo // <- Missing semicolon
 \call($bar); \call($bar);
 </PHP> </PHP>
 +
 +==== Changes to reserved keywords limitations ====
  
 In additional to the namespaced name changes, reserved keywords may now be used in a number of additional places: In additional to the namespaced name changes, reserved keywords may now be used in a number of additional places:
Line 130: Line 134:
 new \List; // Ok! new \List; // Ok!
 </PHP> </PHP>
 +
 +This also extends to function-like language constructs:
 +
 +<PHP>
 +function print($arg) {}
 +
 +print($arg); // This is the normal "print" language construct.
 +\print($arg) // This calls the print() function defined above.
 +</PHP>
 +
 +To avoid confusion in this particular case, it would be possible to prevent defining function symbols that clash with function-like language constructs.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/namespaced_names_as_token.txt · Last modified: 2020/07/31 12:54 by nikic