rfc:keywords_as_identifiers
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | rfc:keywords_as_identifiers [2013/09/18 11:50] – + Version bwoebi | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== PHP RFC: Extended keyword support ====== | ||
+ | * Version: 1.1 | ||
+ | * Date: 2013-09-14 | ||
+ | * Author: Bob Weinand, bobwei9@hotmail.com | ||
+ | * Status: Under Discussion | ||
+ | * First Published at: http:// | ||
+ | ===== Introduction ===== | ||
+ | |||
+ | This RFC aims to remove some restrictions which are due to the fact that keywords are not included in T_STRING (especially for class const, method and class names). | ||
+ | |||
+ | ===== What is now possible ===== | ||
+ | |||
+ | * One might want to define an HTTP agent class. And would like to store some HTTP status constants: | ||
+ | <code php> | ||
+ | class HTTP { | ||
+ | const CONTINUE = 100; // Works with patch | ||
+ | // But currently this fails with a parse error, because continue is a keyword. | ||
+ | const SWITCHING_PROTOCOLS = 101; | ||
+ | // etc. ... | ||
+ | } | ||
+ | </ | ||
+ | * Building conditions with " | ||
+ | <code php> | ||
+ | class Cond { | ||
+ | public static function and (...) { // using an " | ||
+ | // some logic here | ||
+ | return $andCondition; | ||
+ | } | ||
+ | |||
+ | public static function or (...) { | ||
+ | // some logic here | ||
+ | return $orCondition; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Cond:: | ||
+ | </ | ||
+ | * Prevents unnecessary use of _****_call magic method (" | ||
+ | <code php> | ||
+ | // Ugly, current code | ||
+ | class SomeClass { | ||
+ | private $list = []; | ||
+ | |||
+ | public function _list () { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | public function __call ($func, $args) { | ||
+ | if (method_exists($this, | ||
+ | return call_user_func_array(" | ||
+ | } | ||
+ | // some error handling here | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // How we could write it with the patch | ||
+ | class SomeClass { | ||
+ | private $list = []; | ||
+ | |||
+ | public function list () { | ||
+ | return $this-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Details of proposal ===== | ||
+ | |||
+ | Concretely this patch enables: | ||
+ | * all keywords for | ||
+ | * method names | ||
+ | * class constant names | ||
+ | * declare directive names | ||
+ | * a limited set of keywords ***** for | ||
+ | * class names | ||
+ | * trait names | ||
+ | * interface names | ||
+ | * goto label names | ||
+ | |||
+ | ***** This includes all keywords except | ||
+ | * catch | ||
+ | * finally | ||
+ | * case | ||
+ | * default | ||
+ | * exit | ||
+ | * else | ||
+ | * elseif | ||
+ | * endif | ||
+ | * endwhile | ||
+ | * endfor | ||
+ | * endforeach | ||
+ | * enddeclare | ||
+ | * endswitch | ||
+ | It is necessary to exclude them due to limitations of bison/yacc grammar. | ||
+ | |||
+ | An example what is possible with this patch: | ||
+ | <code php> | ||
+ | namespace Test { | ||
+ | class List { | ||
+ | const default = 0; | ||
+ | public $case = array(array(self:: | ||
+ | |||
+ | public static function echo (List $instance) { | ||
+ | var_dump($instance-> | ||
+ | } | ||
+ | |||
+ | public function new (array $entry) { | ||
+ | $this-> | ||
+ | return $this; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | namespace { | ||
+ | \Test\List:: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Typehints ==== | ||
+ | |||
+ | Currently when using array or callable as typehint, the old behaviour is preferred over comparing if it's a class/ | ||
+ | I'm not sure if we should just allow both in this special case. | ||
+ | ===== Proposed PHP Version(s) ===== | ||
+ | |||
+ | This RFC should go into next PHP 5.x. | ||
+ | |||
+ | ===== Patch ===== | ||
+ | |||
+ | * The patch is against master | ||
+ | * Pull request is at [[https:// | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | * Mailing List thread at [[http:// | ||
+ | |||
+ | ===== Rejected Features ===== | ||
+ | |||
+ | * Initially the patch contained also some support for functions, namespaces and constants which was removed later due to some resulting syntactic inconsistencies | ||
+ | |||
+ | ===== Versions ===== | ||
+ | * 1.0: Initial proposal (16.9.2013) | ||
+ | * 1.1: Added some more examples (18.9.2013) |
rfc/keywords_as_identifiers.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1