php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55068 Namespace importation order seems relevant
Submitted: 2011-06-29 11:42 UTC Modified: 2011-07-04 11:09 UTC
From: frederic dot hardy at mageekbox dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.6 OS: Ubuntu
Private report: No CVE-ID: None
 [2011-06-29 11:42 UTC] frederic dot hardy at mageekbox dot net
Description:
------------
1) Create the file named main.php (see test script section).
2) In the same directory of main.php, create a file named ns_class2.php (see test script section).
3) In the same directory of main.php, create a file named ns_ns1_ns2_class1.php (see test script section).
4) In the same directory of main.php, create a file named ns_ns1_ns2_class2.php (see test script section).
5) Execute php -n main.php in CLI.
6) Invert require_once 'ns_ns1_ns2_class2.php' and require_once 'ns_ns1_ns2_class1.php' in main.php.
5) Execute php -n main.php in CLI.

Test script:
---------------
file main.php :
<?php

require_once 'ns_class2.php';
require_once 'ns_ns1_ns2_class2.php';
require_once 'ns_ns1_ns2_class1.php';

?>

file ns_class2.php :
<?php

namespace ns;

class class2 {}

?>

file ns_ns1_ns2_class1.php :
<?php

namespace ns\ns1\ns2;

use ns\class2;

class class1 {}

?>

file ns_ns1_ns2_class2.ph :
<?php

namespace ns\ns1\ns2;

class class2 {}

?>

Expected result:
----------------
Nothing in all cases.

Actual result:
--------------
When ns_ns1_ns2_class2.php was included before ns_ns1_ns2_class1.php :
Fatal error: Cannot use ns\class2 as class2 because the name is already in use in /home/fhardy/tmp/ns_ns1_ns2_class1.php on line 5

When ns_ns1_ns2_class2.php was included after ns_ns1_ns2_class1.php :
Nothing

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-01 19:39 UTC] felipe@php.net
-Package: *Programming Data Structures +Package: Scripting Engine problem
 [2011-07-04 11:09 UTC] dmitry@php.net
-Status: Open +Status: Bogus
 [2011-07-04 11:09 UTC] dmitry@php.net
This is not a bug. It's the expected behaviour.

In case a class is already defined you can't "use" another class with the same name. But on the other hand "use" statement has effect only on current PHP file so you can "use" class in some file and declare it later.
 [2014-03-13 03:31 UTC] tuan dot do at plutus dot vn
IMHO, this is not expected behavior for following reasons:

+ ns\ns1\ns2\class1 should be able to "use ns\class2" without knowledge about ns\ns1\ns2\class2. Because it may not have that knowledge at all.

+ main.php should be able to use ns\ns1\ns2\class1 without knowing about the "use". Because that is the implementation details of ns\ns1\ns2\class1 which should be isolated.

+ It violate the definition that "Importing rules are per file basis". In Frederic's test script above, "use" in ns\ns1\ns2\class1 cause error in main.php, but do not cause error in modified test script below.

Test script:
------------
file main.php: not used

file ns_ns1_ns2_class1.php:
<?php

namespace ns\ns1\ns2;

require_once 'ns_ns1_ns2_class2.php';
require_once 'ns_class2.php';

use ns\class2;

class class1 {}
?>

Expected result:
----------------
Execute ns_ns1_ns2_class1.php. Expect 'Fatal error: Cannot use ns\class2 as class2 because the name is already in use'

Actual result:
--------------
No thing.
Switch order between included files and with the use statement: Nothing.

PS: my environment is PHP 5.5.3 bundled with XAMPP 1.8.3 on Windows 8.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 00:01:31 2024 UTC