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 revision
Next 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://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:
 +<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. ...
 +}
 +</code>
 +  * Building conditions with "and" or "or" methods
 +<code php>
 +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);
 +</code>
 +  * 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)
 +<code php>
 +// Ugly, current code
 +class SomeClass {
 +    private $list = [];
 +
 +    public function _list () {
 +        return $this->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 $this->list;
 +    }
 +}
 +</code>
 +
 +===== 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::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)));
 +}
 +</code>
 +
 +==== 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
 +
 +===== 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