Bug #80224 [Com]: namespaced import alias inconsistent
| From: | rowan dot collins at gmail dot com | Date: | Mon, 12 Oct 2020 11:56:31 +0000 |
| Subject: | Bug #80224 [Com]: namespaced import alias inconsistent | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229568@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80224&edit=1
ID: 80224
Comment by: rowan dot collins at gmail dot com
Reported by: xigal18570 at jarilusua dot com
Summary: namespaced import alias inconsistent
Status: Open
Type: Bug
Package: *General Issues
Operating System: linux
PHP Version: 8.0.0rc1
Block user comment: N
Private report: N
New Comment:
Notably, if the parse error is removed from the test script, the "use\Foo\Bar3" gives a
fatal runtime error of "undefined constant", because that's what PHP has tried to
interpret it as.
Even defining a class with "namespace use\Foo { class Bar3 {} }" does not lead to valid
code, it requires "namespace use\Foo { const Bar3=42; }", which seems incredibly unlikely.
Since "runtime" here means "as soon as the file is included", the only downside
of the current behaviour is that static analysis can't flag it as definitely invalid; but an
opinionated tool could safely warn that it is an almost certain mistake.
Previous Comments:
------------------------------------------------------------------------
[2020-10-12 11:19:49] nikic@php.net
> use\Foo\Bar3; // valid
While this is valid, it's actually a namespace reference, not a use declaration.
I think the correct action here is to forbid the use of a leading \ in use declarations, which
should never have been allowed. This will need a deprecation first though, so it's not
actionable for PHP 8.0.
------------------------------------------------------------------------
[2020-10-12 11:14:41] xigal18570 at jarilusua dot com
Description:
------------
Maybe the description is missing here:
https://wiki.php.net/rfc/namespaced_names_as_token
Test script:
---------------
<?php
use \Foo\Bar2; // valid
use\Foo\Bar3; // valid
use \Foo\Bar4 as Bar44; // valid
use\Foo\Bar5 as Bar55; // invalid
Expected result:
----------------
last variant being valid as well
Actual result:
--------------
$ sudo docker-compose run --rm --user "$(id --user):$(id --group)" php php test.php
Parse error: syntax error, unexpected token "as" in /app/test.php on line 6
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80224&edit=1