Req #47472 [Sus->Wfx]: Namespaces importing and usage

From: Date: Fri, 24 Jun 2016 20:46:55 +0000
Subject: Req #47472 [Sus->Wfx]: Namespaces importing and usage
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201836@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=47472&edit=1 ID: 47472 Updated by: requinix@php.net Reported by: belliash at gmail dot com Summary: Namespaces importing and usage -Status: Suspended +Status: Wont fix Type: Feature/Change Request Package: *General Issues Operating System: Gentoo Linux PHP Version: 5.3.0beta1 Block user comment: N Private report: N New Comment: 1. (re: OP) That code is impossible because whichHand() would be ambiguous: is it calling the function from Utils\Left or from Utils\Right? Aliasing is not a runtime operation that "updates" the local symbol table or something similar - it is used to resolve names at compile-time. 2. (re: @jeffreytgilbert) "use" is not an namespace import. You "use" an individual class. "use Minnow\Framework\Startup" and "Startup::launchApplication()" is the solution. 3. Functions can be use-d as of PHP 5.6. https://wiki.php.net/rfc/use_function https://3v4l.org/cYH4r 4. Importing entire namespaces can't be supported: PHP has no way to know what is available in a namespace before it's been loaded. Consider: <?php spl_autoload_register(function($class) { // ... }); use Foo\*; // because "use Foo" would alias the \Foo class use Bar\*; // or perhaps "import Bar" new Baz(); ?> Which class would that try to autoload? \Baz? Foo\Baz? Bar\Baz? There's no way for PHP to know. Attempting to autoload every possible class would be a large performance hit that only gets worse with each namespace being imported. And what if Foo and Bar both defined a class with the same name? It's perfectly acceptable to do that, but trying to import both namespaces would cause inconsistent behavior at best and errors at worst. (Which has also been said on the mailing list: http://news.php.net/php.internals/68526) So I'll go out on a limb and say this won't happen. Previous Comments: ------------------------------------------------------------------------ [2016-06-24 19:12:24] cmb@php.net Indeed, implicitly importing all symbols of a namespace as can be done in Java with import mynamespace.*; is not implemented in PHP. This would require the RFC process[1], though, so I'm suspending this ticket. [1] <https://wiki.php.net/rfc/howto> ------------------------------------------------------------------------ [2012-10-26 20:58:57] jeffreytgilbert at gmail dot com 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(); } ------------------------------------------------------------------------ [2009-07-02 20:46:32] qwerty763jd at php dot org Huh, You released 5.3 and even does not check this bug report. You FAIL ------------------------------------------------------------------------ [2009-02-22 11:07:28] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=47472&edit=1

« previous php.bugs (#201836) next »