5

Why do namespaces, in PHP, use a backslash () to seperate folders?

E.g:

namespace Core\fruit\Apple;

Are there any particular reasons for this?

I feel the backslash convention is illogical because unix/linux file systems utilize forward slashes. But my opinion is beside the point.

chrisjlee
  • 21,691
  • 27
  • 82
  • 112

1 Answers1

4

Here's why PHP decided to use backslash for namespaces: https://wiki.php.net/rfc/namespaceseparator

Criterions
(1) type-ability (how easy is it to type the separator)
(2) typo-vulnerability (how easy is it to make a typo and get an unwanted behavior without a error/warning)
(3) parse-ability (how easy is it to read the code and figure out whats going on without getting confused with similar syntax that means another thing)
(4) IDE compatibility (5) number of chars

An article expressing an interesting opinion is a good read also: http://www.newmediacampaigns.com/page/php-namespaces-backslash-example

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • 3
    Notably the options even considered for use a separators all suck: `\ ` , `**`, `^^`, `%%`, `:> `, `:)`, `:::`. I say 'suck' because they are almost all two-characters or longer, use special characters, or are the common escape character. No wonder backslash was chosen, no actually nice to use options were even proposed. – ThorSummoner Dec 07 '15 at 22:21
  • 3
    I would have liked if they used dot. – zedfoxus Dec 08 '15 at 01:36
  • 1
    @ThorSummoner yeah, it's "funny" to see that any marginally sane options weren't even considered. The forward slash should have been the first option, even before backslash. And they even missed the criteria of compatibility, as in "Is the symbol in question being used in some other places in some totally different context?". Backslash being the arch-escape character this should have been considered. Crazy! – Tylla Apr 10 '18 at 08:37