php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71079 Namespace collision only when executed with PHP CLI
Submitted: 2015-12-10 11:11 UTC Modified: 2016-10-06 23:06 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: nhuriewi at gmail dot com Assigned:
Status: Duplicate Package: CGI/CLI related
PHP Version: 5.6.16 OS: Debian 3.16.7 x86_64
Private report: No CVE-ID: None
 [2015-12-10 11:11 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
I have updated the OS. Also, the error has been tested for PHP 5.6.10.
 [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
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.
 [2016-03-08 21:07 UTC] nino dot skopac at gmail dot com
snape kills dumbledore
 [2016-10-06 23:06 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2016-10-06 23:06 UTC] nikic@php.net
Duplicate of bug #66773. The CLI/CGI discrepancy is due to opcache.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Aug 22 15:01:28 2019 UTC