From 7e2f6e7f50bd23b6df9302e36bd29910364d71ea Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 6 Sep 2021 12:18:03 +0200 Subject: [PATCH] Allow using readonly as function name Don't treat "readonly" as a keyword if followed by "(". This allows using it as a global function name. In particular, this function has been used by WordPress. This does not allow other uses of "readonly", in particular it cannot be used as a class name, unlike "enum". The reason is that we'd still have to recognize it as a keyword when using in a type position: class Test { public ReadOnly $foo; } This should now be interpreted as a readonly property, not as a read-write property with type `ReadOnly`. As such, a class with name `ReadOnly`, while unambiguous in most other circumstances, would not be usable as property or parameter type. For that reason, we do not support it at all. --- Zend/tests/readonly_function.phpt | 16 ++++++++++++++++ Zend/zend_language_scanner.l | 6 ++++++ 2 files changed, 22 insertions(+) create mode 100644 Zend/tests/readonly_function.phpt diff --git a/Zend/tests/readonly_function.phpt b/Zend/tests/readonly_function.phpt new file mode 100644 index 0000000000000..0262b3b7348ce --- /dev/null +++ b/Zend/tests/readonly_function.phpt @@ -0,0 +1,16 @@ +--TEST-- +Can use readonly as a function name +--FILE-- + +--EXPECT-- +Hi! +Hi! diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index cc424bbec0a64..56c57a5a08543 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -1718,6 +1718,12 @@ NEWLINE ("\r"|"\n"|"\r\n") RETURN_TOKEN_WITH_IDENT(T_READONLY); } +/* Don't treat "readonly(" as a keyword, to allow using it as a function name. */ +"readonly"[ \n\r\t]*"(" { + yyless(strlen("readonly")); + RETURN_TOKEN_WITH_STR(T_STRING, 0); +} + "unset" { RETURN_TOKEN_WITH_IDENT(T_UNSET); }