namespace A{
class Foo{
public function bar(){
echo "foo bar";
}
}
}
namespace B{
use \A;
$a = new Foo();
$a->bar();
// This results in a fatal error!
}
Instead, you either have to use the whole namespace name or just import classes in a package one by one:
namespace B{
$a = new \A\Foo;
$a->bar();
// outputs foo bar;
use \A\Foo;
$n = new Foo;
$n->bar();
// outputs foo bar;
}
This results in a serious problem when you wish to import an entire sub-namespace. In a real application its unlikely you only have one Foo class in the namespace A, so you have to write multiple lines of use statement to import all these classes, which can be quite annoying.
You may argue that Aliasing is here to serve the purpose, but honestly aliasing is only useful in a really complicated system with namespace as long as 'Application\Model\User\Member\Profile\ProfileField\AboutMe'. In most occasions aliasing is just as much typing as using the entire namespace name, and serves no practical purpose.
You know in Java you can import a package of class using something like 'import javax.swing.*', no idea why in PHP its not possible. Or maybe its already available in PHP 5.5? I dunno.
Edited by Hall of Famer, 02 December 2012 - 12:40 AM.














