php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66862 (Sub-)Namespaces unexpected behaviour
Submitted: 2014-03-09 13:57 UTC Modified: 2016-10-08 15:04 UTC
Votes:10
Avg. Score:4.4 ± 0.7
Reproduced:10 of 10 (100.0%)
Same Version:3 (30.0%)
Same OS:8 (80.0%)
From: celestino dot diaz at gmx dot de Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.5.10 OS: LINUX
Private report: No CVE-ID: None
 [2014-03-09 13:57 UTC] celestino dot diaz at gmx dot de
Description:
------------
PHP v5.5.10

Namespaces have an unexpected behaviour depending on the script loading order.

Directory Structure:
Sample
|_ classA.php
|_ classB.php
|_ Subnamespace
  |_ classA.php


Test 1:
Output:
Fatal error:  Cannot use Sample\Subnamespace\classA as classA because the name is already in use in Sample/classB.php on line 3


Test 2 (changed script oder):
Output:
Sample\classB
Sample\Subnamespace\classA
Sample\classA
Sample\classB
Sample\Subnamespace\classA


Test script:
---------------
Sample\classA.php:
<?php
namespace Sample;
class classA {}

Sample\classB.php:
<?php
namespace Sample;
use Sample\Subnamespace\classA;
class classB {
    public $classA;
    public function __construct() {
        $this->classA = new classA();
    }
}

Sample\Subnamespace\classA.php:
<?php
namespace Sample;
class classA {}


Test 1:
$a = new Sample\classA();
echo get_class($a)."\n";
$b = new Sample\classB();
echo get_class($b)."\n";
echo get_class($b->classA)."\n";


Test 2:
$b = new Sample\classB();
echo get_class($b)."\n";
echo get_class($b->classA)."\n";
$a = new Sample\classA();
echo get_class($a)."\n";
$b = new Sample\classB();
echo get_class($b)."\n";



Expected result:
----------------
Both (Test 1 & 2) should have the same output.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-09 18:10 UTC] celestino dot diaz at gmx dot de
-PHP Version: Irrelevant +PHP Version: 5.5.10
 [2014-03-09 18:10 UTC] celestino dot diaz at gmx dot de
changed PHP version
 [2014-03-16 20:06 UTC] celestino dot diaz at gmx dot de
Missed subnamespace declaration in test script:

Sample\Subnamespace\classA.php:
<?php
namespace Sample\Subnamespace;
class classA {}
 [2014-06-04 11:19 UTC] ajrattink at correct dot net
A similar simpler case:

c.php: namespace B; use A\Item; 
b.php: namespace B; class Item {};

main.php:  require 'b.php'; require 'c.php';  FATAL error
main.php:  require 'c.php'; require 'b.php';  No problem at all

Our system worked fine but unexpectedly started crashing because someone declared a new class that suddenly caused a conflict in an included file elsewhere. 
Namespaces were supposed to just fix that problem. 
I would like that namespaces worked completely static and on a per unit level. The way it is designed suggests that it should work that way...
 [2016-10-08 15:03 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2a75f5026a47099f585e29c5a9d8a2989dab42af
Log: Fix bug #66773, #66862
 [2016-10-08 15:04 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2016-10-17 10:07 UTC] bwoebi@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2a75f5026a47099f585e29c5a9d8a2989dab42af
Log: Fix bug #66773, #66862
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC