php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80224 Forbid use of leading \ in use declarations
Submitted: 2020-10-12 11:14 UTC Modified: 2021-06-24 09:53 UTC
From: xigal18570 at jarilusua dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 8.0.0rc1 OS: linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-10-12 11:14 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-12 11:19 UTC] 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:56 UTC] rowan dot collins at gmail dot com
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.
 [2020-10-12 12:23 UTC] xigal18570 at jarilusua dot com
Thanks for the insights Nikita and Rowan. Feel free to close :)
 [2021-06-24 09:53 UTC] cmb@php.net
-Summary: namespaced import alias inconsistent +Summary: Forbid use of leading \ in use declarations -Type: Bug +Type: Feature/Change Request -Package: *General Issues +Package: Scripting Engine problem
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Sep 21 06:03:36 2021 UTC