views:

706

answers:

6

Can someone explain to me what exactly is so bad about using the backslash as the namespace operator for PHP6? It's impossible to read any discussion about PHP6 these days without someone making some scoffing remark about it. One StackOverflower even said that he gave up PHP because of it.

Yes I know that backslash has special meaning as the escape character inside strings, but it's not really any worse than using ->, or the dot . like in many other languages.

It kind of reminds me of all the mocking of Nintendo when they announced the name of the Wii. Everyone makes a big fuss and then once its out and you're used to it, no one cares and they move on.

So, please enlighten me. What is so bad about it? What would have you suggested instead?

+9  A: 

What's so bad about it: Can you spot the error in the following code?

if(class_exists("namespace1\namespace2\myClass"))
    echo "This will never be true";

What would I have suggested: Unfortunately, '\' is the only single single character available. If PHP6 were mine to design, I would replace all the bitwise operators (^, &, |, ~) with keywords (seeing as they're used so little) and use '|' as the namespace separator. In fact I would suggest lots more simple syntax changes to make PHP easier to read and write, but it's easier to just use Python instead ...

too much php
i know what you're saying, but is writing class_exists("namespace1\\namespace2\\myClass") that much harder?
nickf
i find the "\" key inconvenient to reach on a german keyboard. also, no other language (afaik) uses the backslash as ns-separator leading to yet another way of doing the same thing slightly different.
Schnalle
i dont understand how it is the only character available? Why not use . which doesnt have a special meaning inside a string (or am I mistaken) \ has a special meaning inside a string and has been used.
Toby Allen
Toby, because then every incidence of . would require escaping.
cookiecaper
@Toby, the namespace operator would not be used inside a string most of the time.
nickf
Couldn't you use single quotes so PHP doesn't parse any text in the string literal ?
alex
@alex, yes you could.
SkippyFlipjack
Several other languages use the dot character as a namespace seperator, despite it also being their main OO seperator as well - or, in other words, in different contexts it has different meanings. Is there any reason PHP6 couldn't have used -> or . and checked for context?
Moo
You couldn't use a dot, because in a statement like this: MyNameSpace.MyClass it would then be looking for two constants named "MyNameSpace" and "MyClass" to string-append to each other (probably resulting in some warnings and then this: "MyNameSpaceMyClass"). Similarly, if you had a class and a namespace with the same name, -> would lead to ambiguity.
nickf
+3  A: 

The problem with it is that it's the escape character in almost every other context. This means that people will inadvertently mess it up, but it also makes it hard to read because your eyes are tuned to read a backslash as a meta-character, rather than just another symbol.

I would have preferred three colons, which was actually suggested at some point.

troelskn
By "every other context", do you mean strings and regexes? The full stop has different meanings whether used as an operator (string concatenation), in a regex (matches any character) or in a string (it's full stop). This doesn't seem to cause any confusion, so why would \ ?
nickf
I meant it in a broader sense actually. Most languages use the backslash as an escape character. I think the \ is different from a. in that it augments the characters following it (At least when it's used for escaping). I agree that *technically* it isn't much of a problem, but my eyes are trained to assign a special meaning to that character.
troelskn
+2  A: 

Moreover, if I wanted a language that reinvented syntax for no particularly good reason, I would use Ruby.

it's not reinventing anything, it's just using a different character than other languages.
nickf
Really? Last I checked PHP looks pretty C++-like. That's the reason I picked it up, it was a good way to transfer my programming knowledge without significantly readjusting my expectations.On this one point, it's a bad decision but it's survivable. But there's no way that anyone who had asked any sizable population of PHP users what they would like could have come to the conclusion that '\' is in-line with what users want.In fact it's totally misaligned with user expectations, which is what people are mad about. This decision seems to reflect no input from PHP coders.
A: 

Well, there are other problem when using "\" as namespace.

  1. It's the escape character. If you have to use \ in "string" and \ in 'string'. I feel someone will mess with it at somepoint. Soon or later. the escap char will catch you.
  2. the '\' is not very well located on every keyboard. On my keyboard I have to use a combination of key that aren't really close to each others and sometimes it's just a pain. While it's not as bad as '^'. In other words, it will not be possible to write fluently code that needs to access namespace on certain keymap.
  3. I remember when they voted it and how absurd the results are. They chose it because it was simpler than the other char and needed less typing. To be honest, it all depends on your keyboard layout.

That's all the reason I can find why it might be a bad thing to use the escape character. To be honest, I'm still waiting for the language that will create his own unicode symboles. So it would give much more flexibility on which operator you can override. Let say in c++ you could write something like.

bool operator ≤ (Dog dog);
//and then do this
if(myDog ≤ thisDog){
}
//Seems useful?
bool operator ≅ thisDog){ 
  // this wouldn't check for equality 
  // but for something close to it
}

Being able to use our own arbitrary operator make much more sense than using + to group things..."∪" would make much more sense...And if you want to get intersection just you "∩" and then people might say.."what if we don't have those char in our font?" I can only answer with: "Find a font that has them!!!!"

Sybiam
A: 

let's assume the following namespace:

jp\nintendo\rvl\testing

do you notice the errors?

the actual (internal) namespace is most likely something like this:

jp
intendo
vl  esting

The solution to this is to always use two backslashes as a namespace seperator similar to how we're using this in windows filenames.

using two backslashes is completely harmles, as it is a escape sequence itself, which expands into 1 single literal backslash, which is the actual namespace seperator.

now, if we use

jp\\nintendo\\rvl\\testing
as a namespace (using 2 backslashes as the seperator) the actual (internal) namespace becomes:
jp\nintendo\rvl\testing

alexanderpas
There are no errors in your example namespace. If that were in a double quoted string, then it'd be different, but it's not.
nickf
A: 

The real question is, why didn't they just put it as / ? I personally hate \ because it's an escape character... that screws everything up for me!

Nick
`/` is the division operator. How does `\` screw everything?
nickf