PHP Namespaces

Posted by – October 26, 2008

I have been seeing alot of complaining lately regarding PHP namespaces, and I thought I would chime in with my (often opposing) views. First off, let me explain the issue.

The new version of PHP will have a new feature, called namespaces. (I wrote about it in my post”PHP 5.3 Feature Preview“. This is a great featured, and one that the community as a whole is excited about. So what is the problem, then?

Well initially PHP was going to use the standard “::” syntax to invoke the namespace. For example:

namespace Foo;
   function bar() {
      echo "Namespace Foo";

However, this became a problem for the parsing engine, as it is the same way to call a static function.

class Foo{
   static function bar(){
      echo "Class Foo";

So instead, PHP changed the syntax to a blackslash. So now, in the first example, you have ‘Foo\bar();’ while in the second, you still have ‘Foo::bar();’ Seems reasonable to me (and the PHP core members) but not to some.

For example, Ninh complains on his blog that if you put the invocation in double quotes, it will interpret things like “\t” as a tab, and that you have to use 2 backslashes. I really don’t see this being a problem. First off, why on earth would you use double quotes? There are no variables or single quotes being used in the string, so one should be using single quotes anyways. That is just good programming practice.

Even if you do use double quotes, there is a tried and true, standard solution to escape the backslash character. It is so obiquitous, that Ninh didn’t have to learn how to use it, he already knew about it, yet is still complaining. His problem with this method? “It looks like crap”. I’m sorry, I though we were using logic to code web applications, not painting a picture. And even at that, why does “::” look awesome, but “\\” look like crap? I don’t get it.

My favorite quote if from the very end of his post. He claims “Last time I checked, the world wasn’t filled with scrawny developers that would come crying to their mommies after getting their first facepalm of ‘AmbiguousInvocationError’.” And yet he is crying to his mommy (or rather, the blogosphere) because if he uses double quotes (which he doesn’t have to) then he has to escape the backslash character (a standard practice) and that’s “ugly”.

So what are your thoughts? Maybe I’m crazy, and this is a huge deal. I just don’t see it.

6 Comments on PHP Namespaces

  1. Ninh says:

    Maybe I’ve not made myself clear enough in my blog post. You see, aesthetics aside, I’d rather have them use a way to denote a namespace separator in a way that is commonly used in other programming languages. This makes it easier to read or learn PHP if you’re already familiar with other languages who use the same notation. With this train of thought, a dot would’ve been perfectly fine by me as well. Unfortunately, this is already being used in PHP for string concatenation in the parse rule: “String DOT String”.

    This is the same reason why I have problems with using the backslash as a namespace seperator seeing as programmers will be more likely to associate it with escaping. This also makes it harder imho to learn a language.

    Not only that, let’s not forget that if you ever were to instantiate your classes through strings via factory method (which is frowned upon by some to begin with), elaborates on some of the issues you’ll have to deal with as well. For instance, in case you write “foo\tBar”, which is valid PHP code, while trying to say namespace foo and member tBar, you’ll get erroneous behaviour that is easily overlooked. Even an IDE wouldn’t be able to save you in such a situation.

    Aesthetics aside, there are plenty of other reasons why I’m complaining here. I hope people don’t forget to read that as well.

  2. Boris says:

    the real problem is that the PHP language masters didn’t just do the right thing and deprecate the use of “::” for static functions. at some point you have to move the language forward, and keep it consistent and beautiful. but, the PHP people chose to instead put an ugly hack in to try to get the best of both worlds. this *is* PHP we’re talking about here – is anyone really surprised?

  3. Nick Ashley says:

    Boris – if they deprecate “::” for static functions, what would they replace it with? There would have to be some other syntax for static functions, unless your suggestion is to remove static functions from the language.

    Nimh – I kind of understand where you are coming from with keeping a familiar syntax across languages, but don’t know how much weight I would put on that. If every programming language had the same syntax for everything, except PHP was the odd man out, then you would definitely have a point. However every language has there own way to do things like string concatenation, testing equality, or ending statements. Adding namespacing to the mix isn’t that big of a deal IMHO.

    I feel that it would be even harder to learn the language when syntax means two different things. People would be confused saying “I thought that was how you accessed a static function, not a namespace!”. With the backslash, you have a standard way to do everything. Also, I feel you SHOULD be using single quotes, thus eliminating the escaping issue. However, the escaping is a standard way to do things when working with double quotes as well. Why would this confuse them?

  4. I find the backslash horribly ugly as well. Sure, that doesn’t mean it won’t work but a in a discussion about naming conventions it’s often less about what works than what seems orderly and sensible because with few precautions anything works.

  5. Nick Ashley says:

    I guess I am just surprised that a (arguably) slightly uglier solution which works in all situations isn’t favored over a (arguably) better looking solution that doesn’t. Sure, many will say it is bad practice to name a static function inside a class the same as a different class in a namespace. However the whole point of namespacing is to allow code from multiple sources to play nice together. Of course a single programmer utilizing only his own code wouldn’t run into any problems with ‘::’, but from what I understand, someone utilizing classes from many sources could run into this problem. Remember that for better or worse, PHP is the language of choice for many copy and paste coders, and I think that is a relevant fact.

  6. James says:

    Double colon is not much better, and harder to type.

    Also looks terrible at the start of a line:


    I’d have loved to see the dot being used, but that isn’t feasible, so backslash is a satisfactory alternative.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>