php.net |  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: dams@php.net Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 7.3.2 OS: Irrelevant
Private report: No CVE-ID: None
 [2019-02-17 10:10 UTC] dams@php.net
Description:
------------
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:
---------------
<?php   

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

Expected result:
----------------
22

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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-17 10:19 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-02-17 10:19 UTC] nikic@php.net
Uses are always relative to the root namespace, you are looking for "use const A\C as D".
 [2019-02-17 10:21 UTC] requinix@php.net
-Status: Not a bug +Status: Open
 [2019-02-17 10:21 UTC] requinix@php.net
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] dams@php.net
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] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-02-17 10:53 UTC] nikic@php.net
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: Tue Apr 16 18:01:30 2024 UTC