Bug #55068 [Com]: Namespace importation order seems relevant

From: Date: Thu, 13 Mar 2014 03:31:48 +0000
Subject: Bug #55068 [Com]: Namespace importation order seems relevant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184768@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55068&edit=1 ID: 55068 Comment by: tuan dot do at plutus dot vn Reported by: frederic dot hardy at mageekbox dot net Summary: Namespace importation order seems relevant Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Ubuntu PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2011-07-04 11:09:40] 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. ------------------------------------------------------------------------ [2011-06-29 11:42:46] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55068&edit=1

« previous php.bugs (#184768) next »