Bug #71079 [Opn]: Namespace collision only when executed with PHP CLI

From: Date: Thu, 10 Dec 2015 11:27:23 +0000
Subject: Bug #71079 [Opn]: Namespace collision only when executed with PHP CLI
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197765@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71079&edit=1 ID: 71079 User updated by: nhuriewi at gmail dot com Reported by: nhuriewi at gmail dot com Summary: Namespace collision only when executed with PHP CLI Status: Open Type: Bug -Package: Reproducible crash +Package: CGI/CLI related Operating System: Debian 3.16.7 x86_64 PHP Version: 5.6.16 Block user comment: N Private report: N New Comment: I have changed the bug package. And to clarify it, this problem has been tested with both PHP 5.6.10 and PHP 5.6.16. Previous Comments: ------------------------------------------------------------------------ [2015-12-10 11:15:52] nhuriewi at gmail dot com I have updated the OS. Also, the error has been tested for PHP 5.6.10. ------------------------------------------------------------------------ [2015-12-10 11:11:00] nhuriewi at gmail dot com 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71079&edit=1

« previous php.bugs (#197765) next »