|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 19:00:01 2025 UTC |
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.