Skip to content

Commit 76348f3

Browse files
nikickrakjoe
authored andcommitted
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.
1 parent 43f0141 commit 76348f3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Zend/tests/readonly_function.phpt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Can use readonly as a function name
3+
--FILE--
4+
<?php
5+
6+
function readonly() {
7+
echo "Hi!\n";
8+
}
9+
10+
readonly();
11+
readonly ();
12+
13+
?>
14+
--EXPECT--
15+
Hi!
16+
Hi!

Zend/zend_language_scanner.l

+6
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,12 @@ NEWLINE ("\r"|"\n"|"\r\n")
17181718
RETURN_TOKEN_WITH_IDENT(T_READONLY);
17191719
}
17201720

1721+
/* Don't treat "readonly(" as a keyword, to allow using it as a function name. */
1722+
<ST_IN_SCRIPTING>"readonly"[ \n\r\t]*"(" {
1723+
yyless(strlen("readonly"));
1724+
RETURN_TOKEN_WITH_STR(T_STRING, 0);
1725+
}
1726+
17211727
<ST_IN_SCRIPTING>"unset" {
17221728
RETURN_TOKEN_WITH_IDENT(T_UNSET);
17231729
}

0 commit comments

Comments
 (0)