php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47472 Namespaces importing and usage
Submitted: 2009-02-22 11:07 UTC Modified: 2016-06-24 20:46 UTC
Votes:14
Avg. Score:4.5 ± 1.1
Reproduced:11 of 11 (100.0%)
Same Version:9 (81.8%)
Same OS:5 (45.5%)
From: belliash at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.3.0beta1 OS: Gentoo Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: belliash at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-02 20:46 UTC] qwerty763jd at php dot org
Huh, You released 5.3 and even does not check this bug report. You FAIL
 [2012-10-26 20:58 UTC] 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();
}
 [2016-06-24 19:12 UTC] cmb@php.net
-Status: Open +Status: Suspended -Package: Feature/Change Request +Package: *General Issues
 [2016-06-24 19:12 UTC] 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>
 [2016-06-24 20:46 UTC] requinix@php.net
-Status: Suspended +Status: Wont fix
 [2016-06-24 20:46 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC