Bug #77628 [Opn->Nab]: Using const AND use in the same scope.

From: Date: Sun, 17 Feb 2019 10:53:57 +0000
Subject: Bug #77628 [Opn->Nab]: Using const AND use in the same scope.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219612@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77628&edit=1 ID: 77628 Updated by: nikic@php.net Reported by: dams@php.net Summary: Using const AND use in the same scope. -Status: Open +Status: Not a bug Type: Bug Package: *Programming Data Structures Operating System: Irrelevant PHP Version: 7.3.2 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2019-02-17 10:36:13] 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:21:13] 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:19:23] 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:10:05] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77628&edit=1

« previous php.bugs (#219612) next »