I have noticed that when I am using namespacing that loading classes dynamically doesn't work the same as when I'm loading them statically. So, for instance, without the use of namespaces the following are equivalent in their action of instantiating a class called FooBar:
$foobar = new FooBar();
and
$classname = "FooBar";
$foobar = new $classname;
However if when using namespacing I have some code like this:
<?php
namespace Structure\Library;
$foobar = new UserService();
$classname = "UserService";
$barfoo = new $classname;
In this case the UserService class's fully qualified name is Structure\Library\UserService and if I use the fully qualified name it works in both cases but if I use just the shortcut name of 'UserService' it only works when instantiated with the static method. Is there a way to get it to work for both?
P.S. I am using an autoloader for all classes ... but I'm assuming that the problem is happening before the autoloader and is effecting the class string that is passed to the autoloader.
::? Issue understanding of 'Global Space'?$foobar=new UserService();echoyour$classname from inside the autoloader, to give us a better clue? As Allender has answered, I imagine that what's happening is that the class name being processed includes the namespace in one instantiation, but doesn't include it in another. Essentially, the last line of your code is akin to writing:$barfoo = new \UserService();, which fails, because the class is not in the global namespace.