|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2015-12-10 11:15 UTC] nhuriewi at gmail dot com
-Operating System: Linux beta 3.16.0-4-amd64
+Operating System: Debian 3.16.7 x86_64
[2015-12-10 11:15 UTC] nhuriewi at gmail dot com
[2015-12-10 11:27 UTC] nhuriewi at gmail dot com
-Package: Reproducible crash
+Package: CGI/CLI related
[2015-12-10 11:27 UTC] nhuriewi at gmail dot com
[2016-03-08 21:07 UTC] nino dot skopac at gmail dot com
[2016-10-06 23:06 UTC] nikic@php.net
-Status: Open
+Status: Duplicate
[2016-10-06 23:06 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 01:00:01 2025 UTC |
Description: ------------ Let's suppose we have 3 files, each one with its own class: ///////////// // X_A.php // ///////////// <?php namespace X; use \Y\B; class A { public static function foo() { echo B::BAR . "\n"; } } ?> ///////////// // X_B.php // ///////////// <?php namespace X; class B { const BAR = "foo"; } ?> ///////////// // Y_B.php // ///////////// <?php namespace Y; class B { const BAR = "bar"; } ?> /////////////////////////////////// /////////////////////////////////// /////////////////////////////////// Now, we save the following TEST_CODE into a file, and execute it with PHP CLI: include('./X_B.php'); include('./X_A.php'); include('./Y_B.php'); use \X\A; A::foo(); It will produce this fatal error: PHP Fatal error: Cannot use Y\B as B because the name is already in use in /path/to/X_A.php on line 5 This is because the name B is already in namespace X for A, so there's an ambiguity as A can't be sure if B refers to \X\B or \Y\B. In fact, this error happens with any order of include() in which X_B.php is included before X_A.php, regardless of Y_B.php. However, the curious thing here is that, when the same TEST_CODE is executed with PHP running as CGI with an HTTP server, this error doesn't happen at all, and A always considers that B refers to \Y\B, regardless of the include() order. The conclusion here is that the behavior shown by CLI and CGI can't be correct at the same time: either there's a collision or there isn't. Even if there's a good reason for that "duality", it should be clearly explained in the documentation, because this can be a quite tricky error, as it only happens in the CLI and possibly in large projects, in which multiple classes with the same name in different namespaces are more frequent. Thanks. Expected result: ---------------- The expected result is that either the name collision happens always, or it doesn't happen in any case. But it shouldn't depend on whether the code is executed using CGI or CLI.