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?
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With