Skip to content

Commit 7a3dcc3

Browse files
committed
Treat namespaced names as single token
Namespace names are now lexed as single tokens of type T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED or T_NAME_RELATIVE. RFC: https://wiki.php.net/rfc/namespaced_names_as_token Closes GH-5827.
1 parent acbf780 commit 7a3dcc3

20 files changed

+182
-41
lines changed

UPGRADING

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ PHP 8.0 UPGRADE NOTES
204204
numbers and numeric strings continue to work as before. Notably, this means
205205
that `0 == "not-a-number"` is considered false now.
206206
RFC: https://wiki.php.net/rfc/string_to_number_comparison
207+
. Namespaced names can no longer contain whitespace: While `Foo\Bar` will be
208+
recognized as a namespaced name, `Foo \ Bar` will not. Conversely, reserved
209+
keywords are now permitted as namespace segments.
210+
RFC: https://wiki.php.net/rfc/namespaced_names_as_token
207211

208212
- COM:
209213
. Removed the ability to import case-insensitive constants from type
@@ -509,6 +513,11 @@ PHP 8.0 UPGRADE NOTES
509513
instead be part of a following T_WHITESPACE token. It should be noted that
510514
T_COMMENT is not always followed by whitespace, it may also be followed by
511515
T_CLOSE_TAG or end-of-file.
516+
. Namespaced names are now represented using the T_NAME_QUALIFIED (Foo\Bar),
517+
T_NAME_FULLY_QUALIFIED (\Foo\Bar) and T_NAME_RELATIVE (namespace\Foo\Bar)
518+
tokens. T_NS_SEPARATOR is only used for standalone namespace separators,
519+
and only syntactially valid in conjunction with group use declarations.
520+
RFC: https://wiki.php.net/rfc/namespaced_names_as_token
512521

513522
- XML:
514523
. xml_parser_create(_ns) will now return an XmlParser object rather than a

Zend/tests/attributes/019_variable_attribute_name.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ class A {}
88

99
?>
1010
--EXPECTF--
11-
Parse error: syntax error, unexpected variable "$x", expecting identifier or "static" or "namespace" or "\" in %s on line %d
11+
Parse error: syntax error, unexpected variable "$x" in %s on line %d

Zend/tests/bug43343.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ $foo = 'bar';
88
var_dump(new namespace::$foo);
99
?>
1010
--EXPECTF--
11-
Parse error: %s error%sexpecting%s"\"%sin %sbug43343.php on line 5
11+
Parse error: syntax error, unexpected token "namespace" in %s on line %d

Zend/tests/bug55086.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace N2 {
2626
echo $a->hello(), PHP_EOL;
2727
echo $a->foo(), PHP_EOL;
2828
try {
29-
} catch(namespace \Foo $e)
29+
} catch (namespace\Foo $e)
3030
{
3131
}
3232
}

Zend/tests/grammar/regression_010.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ Test to check regressions on T_IMPLEMENTS followed by a T_NS_SEPARATOR
55

66
interface A{}
77

8+
// No longer considered legal in PHP 8.
89
class B implements\A {}
910

1011
echo "Done", PHP_EOL;
11-
--EXPECT--
12-
Done
12+
13+
?>
14+
--EXPECTF--
15+
Parse error: syntax error, unexpected namespaced name "implements\A", expecting "{" in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use "namespace" as namespace name, due to conflict with ns-relative names
3+
--FILE--
4+
<?php
5+
6+
namespace NAMEspace;
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Cannot use 'NAMEspace' as namespace name in %s on line %d
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Cannot use "namespace\xyz" as namespace name, due to conflict with ns-relative names
3+
--FILE--
4+
<?php
5+
6+
namespace NAMEspace\xyz;
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected namespace-relative name "NAMEspace\xyz", expecting "{" in %s on line %d
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Reserved keywords in namespace name
3+
--FILE--
4+
<?php
5+
6+
namespace iter\fn {
7+
function test() {
8+
echo __FUNCTION__, "\n";
9+
}
10+
}
11+
12+
namespace fn {
13+
function test() {
14+
echo __FUNCTION__, "\n";
15+
}
16+
}
17+
18+
namespace self {
19+
function test() {
20+
echo __FUNCTION__, "\n";
21+
}
22+
}
23+
24+
namespace {
25+
use iter\fn;
26+
use function fn\test as test2;
27+
use function self\test as test3;
28+
fn\test();
29+
test2();
30+
test3();
31+
}
32+
33+
?>
34+
--EXPECT--
35+
iter\fn\test
36+
fn\test
37+
self\test
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Whitespace between namespace separators is no longer allowed
3+
--FILE--
4+
<?php
5+
6+
Foo \ Bar \ Baz;
7+
8+
?>
9+
--EXPECTF--
10+
Parse error: syntax error, unexpected token "\" in %s on line %d

Zend/tests/ns_096.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ use Foo\Bar\{\Baz};
77

88
?>
99
--EXPECTF--
10-
Parse error: syntax error, unexpected token "\", expecting identifier or "function" or "const" in %s on line %d
10+
Parse error: syntax error, unexpected fully qualified name "\Baz", expecting identifier or namespaced name or "function" or "const" in %s on line %d

0 commit comments

Comments
 (0)