|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-02-22 11:07 UTC] belliash at gmail dot com
Description:
------------
It is impossible to import namespace (to both global scope and to any other namespace).
It is only possible to alias namespaces...
Reproduce code:
---------------
/* utils_left.php */
<?php
namespace Utils\Left;
function whichHand()
{
echo "I'm using my left hand!";
}
?>
/* utils_right.php */
<?php
namespace Utils\Right;
function whichHand()
{
echo "I'm using my right hand!";
}
?>
/* index.php */
<?php
include('./utils_left.php');
include('./utils_right.php');
Utils\Left\whichHand(); // outputs "I'm using my left hand!"
Utils\Right\whichHand(); // outputs "I'm using my right hand!"
use Utils\Left;
whichHand(); // outputs "I'm using my left hand!"
use Utils\Right;
whichHand(); // outputs "I'm using my right hand!"
?>
Expected result:
----------------
I'm using my left hand!I'm using my right hand!I'm using my left hand!I'm using my right hand!
Actual result:
--------------
I'm using my left hand!I'm using my right hand!
Fatal error: Call to undefined function whichHand() in /home/Belliash/HtDocs/test/index.php on line 9
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 09 15:00:02 2025 UTC |
I'm having the exact same issue. PHP 5.4.7. The Namespace is never imported and can only be used when referencing the full or partial namespace path. This isn't close enough to how the expected functionality would work. This makes partially useless because all existing code that didn't call namespaces directly with their full or partial path referencing classes/methods etc wont work. You can't simply wrap your classes in a namespace and have them encapsulated for easy import where appropriate. Errant code: -------------------- <?php namespace Minnow\Framework{ class Startup{ public static function launchApplication(){ echo 'Application launched'; } } } namespace WebRoot{ use Minnow\Framework; Startup::launchApplication(); } Fatal error: Class 'WebRoot\Startup' not found in /Users/jeffreytgilbert/Desktop/Namespace-test-failure-use-namespace.php.php on line 12 Errant code: --------------------- <?php namespace Minnow\Framework{ class Startup{ public static function launchApplication(){ echo 'Application launched'; } } } namespace WebRoot{ use Minnow\Framework; \Startup::launchApplication(); } Fatal error: Class 'Startup' not found in /Users/jeffreytgilbert/Desktop/Namepsace-test-failure-root-namespace.php on line 12 Errant code: <?php namespace Minnow\Framework{ class Startup{ public static function launchApplication(){ echo 'Application launched'; } } } namespace WebRoot{ use Minnow\Framework; Startup::launchApplication(); } Fatal error: Class 'WebRoot\Startup' not found in /Users/jeffreytgilbert/Desktop/Namespace-test-default-namespace.php on line 12 ===================================== -- this is the part that chafes me ===================================== <?php namespace Minnow\Framework{ class Startup{ public static function launchApplication(){ echo 'Application launched'; } } } namespace WebRoot{ \Minnow\Framework\Startup::launchApplication(); } Works fine, which is silly because then it makes the "use" arguments pointless because this still works, which it should, but doesnt work in the global or default scope. <?php namespace Minnow\Framework{ class Startup{ public static function launchApplication(){ echo 'Application launched'; } } } namespace WebRoot{ use Minnow\Framework; \Minnow\Framework\Startup::launchApplication(); }