Hacker News new | past | comments | ask | show | jobs | submit login
Why does PHP use \ instead of / for namespaces?
6 points by readonthegoapp on June 25, 2021 | hide | past | favorite | 10 comments
e.g.

  namespace Foo\Bar\subnamespace;
instead of:

  namespace Foo/Bar/subnamespace;
it seems like, in 2008/9, when the decision was being made/implemented, the arguments were around whether or not to stick with the '::' operator or find some new symbol.

as a much more casual coder, i find the backslash operator annoying.

and it's the type of aesthetic thing that bothers me more than other things that seem to bother other folks much more -- alleged less than ideal type coercion, etc.

i'm assuming using the forward slash, '/', was just a non-starter, but....was it?




Here's where the decision was made:

Request for Comments: Namespace Separators - https://wiki.php.net/rfc/namespaceseparator

The candidates were:

  \  **  ^^  %%  :>   :)  :::
The IRC dicussion:

https://wiki.php.net/_media/rfc/php.ns.txt (Downloads .TXT file)


thanks, the IRC record is awesome - wish all the pastebins still worked.

i did read that page about the candidates -- it just seemed like the most obvious separator, '/', was not in that list, and it made me wonder.


It's already division


They couldn’t use forward slash, as it is the divide operator.

They couldn’t use a period, like python or java, because they use it as the concatenation operator.


Can you have a namespace with the same name as a local variable? If not, then they could have used `/`. Of course then you cannot know what an expression means without executing the program, but I'd assume they already have a few cases in there (with variable variables and so on; it's bin a while since I've used PHP productively).

It would also have been possible to just reuse `->`, then namespaces look a lot like static objects. Or the double colon `::` which was already used in some contexts.


PHP 8 changed how the parsing of namespaces work, instead of multiple tokens it is just one.

https://wiki.php.net/rfc/namespaced_names_as_token

I wonder with this change it would be theoretically possible to have any character as a namespace separator.


i see how using the forward slash could have presented problems, but it doesn't seem to be a problem for other languages, so...

i guess part of what really strikes me about it is...it's a _back_slash.

like...something Bill Gates would approve of.

backslashes are not open source-y or unix-y or linux-y.

the 'history of php' shows this snippet:

  <!--include /text/header.html-->
which, to me, just indicates that PHP has been comfortable with forward slashes in all sorts of contexts for a long time, it was built to resemble-ish C, etc. etc.

i thought one of the folks making the decision might have been like:

  Backslash? Nah.
Everyone has their hangups -- right now, mine is:

  OK, I'm gonna use Laravel. I'm gonna use the starter kits, they're free, PHP is doing fine, much better than Python actually/probably, it's prob 1,000x faster than Python, Python still doesn't know what version it wants to be and will prob never get to another version anyways, but... can I really look at those PHP backslashes?? No. No, I can't. I'm using python/django instead. ... We'll see.
I've been thinking about this for months. I figure in a few more months I'll figure out how to disqualify python/django.


When I started coding PHP professionally about ten years ago I was bothered about the namespace separator too, but now it looks natural too me and I think it is more readable than a dot.

On a Swedish keyboard layout it is easier to type backslash than forward slash, so that is a plus for me.

If you use an IDE like PhpStorm you rarely need to type namespaces anyway.


Total guess: Maybe because forward slash is used in paths of web resources, and php-being sometimes mixed with HTML- opted for a namespace syntax that looked distinct from files?


As a former PHP developer primarily, shortsightedness, lol.

Still pretty good lang for web, though.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: