I've been working with PHP for a long time, but am now starting to experiment with newer language features such as namespaces. I have a question regarding autoloading that I haven't been able to find an adequate answer to in my web searching.
Suppose I have classes in different namespaces:
namespace foo\bar\baz;
class Quux
{
}
namespace fred\barney\wilma;
class Betty
{
}
Then suppose I had an autoloader that assumes that there's a 1:1 mapping between namespaces and directory structures:
function autoload ($className)
{
$className = str_replace ('\\', DIRECTORY_SEPERATOR, $className);
include ($className . 'php');
}
spl_autoload_register ('autoload');
Does the fully qualified namespace get passed to the autoloader under all circumstances, or does the autoloader need to take the namespace currently being used into account?
For example, if I do the following:
$a = new \foo\bar\baz\Quux;
$b = new \fred\barney\wilma\Betty;
the autoloader should work fine.
But what if I do the following?
use \foo\bar\baz as FBB;
$a = new Quux;
$b = new \fred\barney\wilma\Betty;
When attempting to instantiate a new Quux, will the autoloader still get \foo\bar\baz\Quux
as the class name argument? Or should it get FBB\Quux
, or even just Quux
?
If the latter, can I determine the namespace the class is supposed to be in from within my autoloader by using __NAMESPACE__
or some other such mechanism?