PHP continues its descent into irrelevance • Gated Logic • nevali.net

One of the most eagerly-anticipated features of PHP 6 was namespaces: so eaglery-anticipated, in fact, that many seasoned developers had wondered why on earth it took until PHP 6 for it to be implemented. There was some respite, however, in that it became slated for inclusion as part of PHP 5.3, along with several other language features which make life a little easier for developers making heavy use of PHP’s OO features.

I should preface this post by stating that I’m paid to develop software in PHP. I’ve been writing in PHP for years, and quite enjoy it. Having said that, this doesn’t automatically mean I have to like the direction things are going, nor that I’ll keep building things in PHP forever.

Much of PHP’s syntax is modelled on C/C++. The scope operator is ::, and the object accessor is ->. Except that’s not strictly true. :: is the scope operator only when referring to static members of classes: the decision was taken a couple of days ago to make the namespace scope operator a backslash. As in, the universal escape character.

The reason for not using :: was ambiguity between accessing static members of a class in the current namespace, and accessing members of a different namespace with the same name. Presumably the PHP development team (with the exception of dmitry, who opposed the switch to a backslash) are unaware of the concept of rules of precedence. The rule would be straightforward:

  1. If an unqualified scoped name (i.e., one which didn’t begin with :: to indicate that it’s relative to the root scope) is to be resolved, attempt to resolve it first against the current namespace.

That’s it. Therefore, if you have a namespace named Foo and a class named Foo within the namespace Bar, any code executing within the context of Bar would treat Foo::thing as referring to the thing member of the Foo class within the Bar namespace. If you want to refer to the Foo namespace explicitly from within a namespace which has a class named Foo, you can write it as ::Foo (and if it has a member named thing, it would be ::Foo::thing). Simple.

The use of backslash as a namespace scope operator wouldn’t be so bad if it wasn’t for the fact that class and method names get passed around in strings all the time in PHP; moreover, pretty much every programmer’s editor that deals with PHP already understands :: but treats backslash as something completely different.

Am I happy that PHP is getting namespaces, at long last? Sure. Am I likely to use them extensively given how confusing they’ll make my code look? Not especially.