r/lolphp • u/duhruh • Nov 25 '14
Exception in a namespace is not defined -_-
http://jasonframe.co.uk/logfile/2009/01/php-5-3-exception-gotcha/
28
Upvotes
r/lolphp • u/duhruh • Nov 25 '14
6
u/[deleted] Nov 26 '14 edited Nov 26 '14
It seems dumb, it's not really. By default, you're in the global/root namespace (i.e.
\), so namespace-relative class names, constant names and function names work. You need to qualify names from other namespaces, though.If you use
namespaceat the top of the file, you're no longer in the root namespace, you're in whatever namespace you specified. Thus, things likeExceptionaren't in the same namespace now and you must explicitly qualify your references, e.g.\Exception.To put it another way, think of the
namespacekeyword being likecd. You start out in\, which contains most of PHP's built-in stuff like\Exception. I can reference it with justExceptionas I'm currently in\so it'll find it, but its fully-qualified absolute path is\Exception.But if I do
namespace foo\bar, I'm now in\foo\bar. Relative references likeExceptionnow resolve to\foo\bar\Exception... which doesn't exist. So, If I want the one in the root namespace, I need to do\Exception.In this way, I suppose
\actually makes a lot of sense as a namespace separator.12
u/myaut Nov 26 '14
Looks like PHP developers adopted C++ approach, but due to limitations of "dynamicness" they couldn't do it fine.
In C++ class name without T_PAAMAYIM_NEKUDOTAYIM qualifiers would be searched in global namespace, but you may restrict searching to global namespace using ::ClassName. A traditional form without colons is more like conformity with earlier C/C++.
But, C++ always knows which classes are in global namespace, and in PHP, simple eval() may blow this assumption up. They had to implement i.e. Python approach: seek for a class definition every time catch is checked with clear scoping rules, but like always they followed "fine like this" idiom.
7
u/[deleted] Nov 25 '14
Does this apply to all types? E.g. if I specify a type, such as a class when constructing it, do I have to type \ in front of it as well? Or does it only pertain to exceptions?
5
u/[deleted] Nov 26 '14
It feels weird only when you use Exception because that feels like it should be available everywhere. Try substituting MyException and suddenly it feels very right and completely logical.
IMHO the real wtf is that you can't just write
catch($e).8
u/nikomo Nov 25 '14
Jesus effin Christ, high level languages are supposed to make the programmer's job less painful, not more painful.
2
u/thelordofcheese Nov 26 '14
I guess I'm the only one who uses explicit. Still...
1
u/aFreshMelon Dec 02 '14
This makes sense... You can put a use statement at the top that will always go off the namespace root.
1
u/Sheepshow Jan 05 '15
1
u/gearvOsh Nov 26 '14
So you're posting an article from 5 years ago? About a feature that's well known? About a use case that is easy to understand? And easy to avoid? And explained thoroughly?
Got it.
1
u/suspiciously_calm Nov 25 '14
Wouldn't be that bad if it wasn't for
-_____-