|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77628 Using const AND use in the same scope.
Submitted: 2019-02-17 10:10 UTC Modified: 2019-02-17 10:53 UTC
From: Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 7.3.2 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
New email:
PHP Version: OS:


 [2019-02-17 10:10 UTC]
Using an import alias and defining the constant in the same scope leads to inconsistent error message :  Undefined constant 'C' 

C is aliased into D first. 
Then it is created.
Its creation is validated by 'echo C', which yields the right value.
'echo D' fails, mentioning that 'C' doesn't exists.

Extra note : 
+ splitting the namespace A in two (one with const, one with use) raise the same issue.
+ use and const may be in any order (use first, const first), still raise the same issue.

Test script:

namespace A {
    use const C as D;
    const C = 2;
    echo C;
    echo D;

Expected result:

Actual result:
2PHP Fatal error:  Uncaught Error: Undefined constant 'C' in test.php:8
Stack trace:
#0 {main}
  thrown in test.php on line 8


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-17 10:19 UTC]
-Status: Open +Status: Not a bug
 [2019-02-17 10:19 UTC]
Uses are always relative to the root namespace, you are looking for "use const A\C as D".
 [2019-02-17 10:21 UTC]
-Status: Not a bug +Status: Open
 [2019-02-17 10:21 UTC]
It took me a second to understand but the code is correct. @dams is talking specifically about the error message (which points to the "echo D" line) saying "C" when it would be better if it said "D".
 [2019-02-17 10:36 UTC]
Yes to both comments.

When C is defined in the global space (in the example), it works as expected.

The error message mentions C, which is confusing with D.
 [2019-02-17 10:53 UTC]
-Status: Open +Status: Not a bug
 [2019-02-17 10:53 UTC]
The error message is also correct: It shows the constant PHP is actually looking for, and the constant that needs to be defined for the lookup to succeed. The name "D" used locally to refer to it is ultimately irrelevant.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 13 23:01:29 2024 UTC