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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: xigal18570 at jarilusua dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC