Reputation: 546015
Can someone explain to me what exactly is so bad about using the backslash as the namespace operator? I'v read a lot of scoffing remarks 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?
Upvotes: 13
Views: 1929
Reputation: 316969
The official RFC and backing documents can be found at
They include an IRC log about the decision process.
Quoting Section "Problems"
\
looks a lot like/
and is easy to accidentally flip, especially for unix users\
is used for escaping- inside a string a namespace name becomes
\\like\\this
or we can get weird characters. This could be confusing to users at first.- all existing namespaced code must be nearly rewritten, a simple search/replace of
::
would not be possible.- the patch touches a lot of the engine, and will need rigorous battle-testing.
- to many,
\this\way
will look weird at first.
Any of the scoff remarks you mentioned are likely due to that above or personal opinion for "reasons". I, for instance, find them quite ugly to read and cumbersome to write, especially in strings where I have to use double backslashes. But then again, I get used to it the more often I use them.
Upvotes: 1
Reputation: 1
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!
Upvotes: 0
Reputation: 2772
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\\testingas a namespace (using 2 backslashes as the seperator) the actual (internal) namespace becomes:
jp\nintendo\rvl\testing
Upvotes: 0
Reputation: 13600
Well, there are other problem when using "\" as namespace.
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!!!!"
Upvotes: 1
Reputation:
Moreover, if I wanted a language that reinvented syntax for no particularly good reason, I would use Ruby.
Upvotes: 3
Reputation: 117457
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.
Upvotes: 3
Reputation: 90988
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 ...
Upvotes: 12