rfc:keywords_as_identifiers
This is an old revision of the document!
PHP RFC: Extended keyword support
- Version: 1.0
- Date: 2013-09-14
- Author: Bob Weinand, bobwei9@hotmail.com
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/keywords_as_identifiers
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:
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 “and” or “or” methods
class Cond { public static function and (...) { // using an "and" or "or" as name currently fails // some logic here return $andCondition; } public static function or (...) { // some logic here return $orCondition; } } Cond::and($cond1, $cond2);
- Prevents unnecessary use of __call magic method (“->keyword_name_for_non_static_method()” works, but “public function keyword_name_for_non_static_method() {}” currently fails)
// Ugly, current code class SomeClass { private $list = []; public function _list () { return $list; } public function __call ($func, $args) { if (method_exists($this, "_".$func)) { return call_user_func_array("_".$func, $args); // or some similar thing, if not underscores etc.... } // some error handling here } } // How we could write it with the patch class SomeClass { private $list = []; public function list () { return $list; } }
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:
namespace Test { class List { const default = 0; public $case = array(array(self::default)); public static function echo (List $instance) { var_dump($instance->case); } public function new (array $entry) { $this->case[] = $entry; return $this; } } } namespace { \Test\List::echo((new Test\List)->new(array(1))); }
Typehints
Currently when using array or callable as typehint, the old behaviour is preferred over comparing if it's a class/interface named array or callable. 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://github.com/php/php-src/pull/438
References
- Mailing List thread at http://markmail.org/message/7rn4mbwkbytqa3ig
Rejected Features
- Initially the patch contained also some support for functions, namespaces and constants which was removed later due to some resulting syntactic inconsistencies
rfc/keywords_as_identifiers.1379504892.txt.gz · Last modified: 2017/09/22 13:28 (external edit)