Bug #71079 [NEW]: Namespace collision only when executed with PHP CLI
| From: | nhuriewi at gmail dot com | Date: | Thu, 10 Dec 2015 11:11:04 +0000 |
| Subject: | Bug #71079 [NEW]: Namespace collision only when executed with PHP CLI | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-197766@lists.php.net to get a copy of this message | ||
From: nhuriewi at gmail dot com
Operating system: Linux beta 3.16.0-4-amd64
PHP version: 5.6.16
Package: Reproducible crash
Bug Type: Bug
Bug description:Namespace collision only when executed with PHP CLI
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 bug report at https://bugs.php.net/bug.php?id=71079&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71079&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71079&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71079&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71079&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71079&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71079&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71079&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71079&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71079&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71079&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71079&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71079&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71079&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71079&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71079&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71079&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71079&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71079&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71079&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71079&r=mysqlcfg