Your PHP example is possible to write in a way that is nearly identical to the C# you provided. Why are you prefixing Namespace\MyClass(); when you declared your namespace scope in 'use \My\Namespace'? Your assignment can just be:
$var = new MyClass();
This brings it pretty much in line with your C# example. Have you read the PHP docs on namespaces?That's the point; the PHP doesn't compile without the namespace prefix. In fact I just tried it to make sure I'm remembering right.
I was taken aback when I tested this for myself as my initial reading of the PHP docs lead me to a very different conclusion. You are indeed correct, and in my opinion that is very broken. Touché.
Haven't worked with the PHP namespaces a lot myself yet but there seem to be more convenient ways:
http://www.rooftopsolutions.nl/blog/taking-advantage-of-php-...
And a good read on PHP namespaces in general: http://weierophinney.net/matthew/archives/254-Why-PHP-Namesp...
You can't import everything from a whole namespace, but you can do it on a per-class basis. The following works as expected:
namespace My {
class MyClass { public function Foo() { echo 'Foo'; } }
}
namespace {
use My\MyClass;
$var = new MyClass();
$var->Foo();
}
Plus the weird exceptions required for backwards compatibility, and the global namespace: I often forget where a root \ is required and where it isn't (not when declaring a namespace, but often--not always!--when referencing one). And of course there's the monstrous choice of namespace separator.
End of the world? Not really. Sucks? Yeah. And possibly a dealbreaker for me for larger projects with complicated namespaces and lots of classes.
I'd love for this to be just a misunderstanding on my part, because it would make my life a lot easier if PHP namespaces where more like C# ones!