Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Namespace operator... what's the big deal?

Tags:

namespaces

php

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?

like image 372
nickf Avatar asked May 07 '09 05:05

nickf


2 Answers

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 ...

like image 92
too much php Avatar answered Oct 20 '22 19:10

too much php


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.

like image 40
troelskn Avatar answered Oct 20 '22 18:10

troelskn