views:

1495

answers:

19

PHP 5.3 (alpha) has had namespace support for a while using '::' as a separator. But due to some problems with ambiguity with this separator, the PHP team has decided on '\'.

From a recent story on Slashdot:

PHP finally is finally getting support for namespaces. However, after a couple hours of conversation, the developers picked '\' as the separator, instead of the more popular '::'. Fredrik Holmström points out some problems with this approach. The criteria for selection were ease of typing and parsing, how hard it was to make a typo, IDE compatibility, and the number of characters.

Who else thinks '\' is one of the worst separators? '\' is almost universally accepted as an escape character, but besides that it just looks unnatural.

$foo = new \Foo\Bar();
+10  A: 

\Foo\Bar() doesn't look any more unnatural than /Foo/Bar(), which looks completely natural to me for describing a tree structure.

...or perhaps I've been using *nix for too long.

Also, this is outside any sort of quoting structure, so I don't see a problem with people mistaking it for an escape character. Personally, I have far more issues with escape characters in functions like preg_*.

Matthew Scharley
I'd also much rather see '/'. Developing PHP for *nix just makes more sense to me than using '\'.
Dan Herbert
The problem with "/" is it already denotes another operation.
Milen A. Radev
I'm going to follow along with everybody else on the it's different from every other language and it doesn't make sense to me since it already concatenates escaping in a string.
jtyost2
The "it's different to every other language" is a non-argument. Not every language uses the same syntax for this operation. The problem with it being used for escaping in strings may prove to be a issue in the 'getting used to' stages. I stand by my statement about preg_* being far worse.
Matthew Scharley
php doesn't have operator overloading. its not a problemNamespace / Class != meaningful in context of division.
Kent Fredric
+4  A: 

Within the context of PHP, I don't see how this could be confused with anything else. It's also reminiscent of a file path in MS-DOS, so it makes sense in my head.

Escaped characters would always be within a string, so that shouldn't be confusing. And it's actually nice not using '::' so that it's easier to differentiate static methods from classes.

Lucas Oman
A: 

I think it's just going to lead to a lot of confusion, especially with novice developers who are just picking up PHP. It's hard enough to get them to write clean, elegant code now, I dread to think what'll happen once that gets introduced.

JamShady
+32  A: 

Of course the issue is down to personal preference (as it always is with syntax). But I think there are a few of problems.

  1. '\' is completely new. Every other language uses '::' or '.'.
  2. It is the escape character in (almost) every language. Of course escape characters only matter in strings, but my brain will always see it as an escape character. Imagine how it will look to newbies when they realize 'Foo\Bar' does NOT have to be escaped to 'Foo\\Bar'.
  3. How many people will continuously type '/' by accident? I haven't used Windows in years, having the namespace path resemble a Windows file path looks wrong when I glance over it.
  4. It really does look ugly to me. I work with PHP 8 hours a day, I think I deserve to be a little shallow ;-)
Christopher Nadeau
#2: It does if it's embedded in a string: echo "Stuff like $Foo\\Bar is screwed up." I think. Or maybe they added yet another special case.
Just Some Guy
Sometimes I feel as if the PHP-developers are determined to make sure that PHP consolidates it's reputation as the ugly-duckling of the computer languages.
Jacco
I am just learning to use namespaces so honestly for me the Windows file path look-a-like kind of makes it easiar for me to follow as I can imagine namespaces in a way I do a filesystem. Also if they would have used :: wouldn't that have broken existing PHP code that calls static methods like classname::myMethod() ?
jasondavis
+8  A: 

I think the bigger WTF is not the the choice of a separator. I think it is the fact that their lexical parser can't handle any form of context ambiguity.

For example, in C

 int foo (int bar);

I'm using int in different contexts here, but it knows the difference...Is PHP so dumb that it couldn't figure out when :: meant one thing over the other?

Also, factory methods that use strings to represent fully qualified names will now need to be double escaped.

 $className = "Root\\MyNameSpace\\MyClass";

All in all, nothing really surprises me with PHP now.

FlySwat
there is ambiguity there. "Foo::Bar::Fubar()" ... is that calling the static function Bar::Fubar() in the Foo namespace, or is it calling the function Fubar() in the Foo::Bar namespace? Foo\Bar::Fubar() or Foo\Bar\Fubar() is much clearer.
nickf
The ambiguity only comes up if you have both a 'Bar' namespace and a 'Bar' class in the same parent 'Foo' namespace. Which is a dumb thing to do anyway, I wouldn't mind it throwing an error if I did that.
davr
+2  A: 

Re: Jonathan Holland

There would be ambiguities with :: as separator. There are none with a backslash. Your example from C doesn't compare, because there are no ambiguities with the use of int -- The context makes the meaning apparent. This is not the case with the ::.

Now, the ambiguities are rare and - as far as I can see - would always be a result of bad naming style anyway, so I wouldn't mind the ambiguity. In fact I would prefer virtually anything other than backslash.

See also: A word on PHP’s upcoming namespace separator

troelskn
+2  A: 

I have a couple of thought on this:

  1. Now that PHP coding is my fulltime job, I begin to worry about how simple (a 2 hour irc-chat between 'the powers that be') the workings of a major new feature of the language gets decided on.
  2. I think they'll scratch their head, acknowledge that they need to lay off the beer during these IRC chats, and just revert back to the C-style :: operators. The parser should be smart enough to make the difference between calling a static method and accessing a package.
SchizoDuckie
hmmmm.... beeeeer....
bastianhuebner
Exactly how would you go about differentiating them?Can you tell, looking at Zig::Zag::Zoom() which is which?
eyelidlessness
Simple, its whatever was defined. Classes should not exist in the same lexical scope as namespaces with the same name. Its *stupid*
Kent Fredric
Yeah, this is basically my beef with PHP in general - a frequently evolving language is being directed with a "guys in their basement" approach. I think I had enough around the time that they started flipping magic quotes on and off and then on again between minor versions. Put some thought into it!
David
+2  A: 

Here is the meeting IRC chat log which explains their conclusion for selecting this new operator: http://wiki.php.net/_media/rfc/php.ns.txt?id=rfc%3Anamespaceseparator&cache=cache

Kevin
+8  A: 

as a new PHP developer who is starting from 5.2 and using Iterators, Classes, and looking forward for closures and anonymous functions in 5.3 -- I really dislike the choice for the namespace separator.

I rely on eval, syntax highlight and my tools to reprocess PHP files (combine them together/etc). Having that choice in PHP is horribly incosistent with existing programming conventions across programming languages.

Also, the fact there can be only one name space per file is a very significant drawback, it feels that neither the PHP development team, nor the existing parser are ready for more sophisticated features, and instead they are being jammed in.

It is obvious that PHP has been grown incrementally, and it is reflected in built-in functions, their naming conventions/etc. While the plethora of inconsistent naming conventions in functions, overlapping functionality, and inconsistent arguments are 'dealt' with with the understanding of 'growing pains' -- the Language syntax features cannot be handled the same way.

Basically if a fundamental feature cannot be 'completely and cleanly' introduced -- it should not be put in, untill the necessary machinery develops to include it.

Vlad

You can have multiple namespaces per file.
Chacha102
+5  A: 

The price we have to pay is too high, as in the end it causes more problems than the previous separator.

It tries to solve the ambiguity problem, but I don't see any kind of problem here. As troelskn points out in a previous post, they are mostly caused by bad naming conventions. If someone has equally named namespaces and methods, he must be doing something evil. Calling foo::bar::baz() was far more intuitive than calling foo\bar::baz().

I fear that the PHP devs are not going to hear us again. Something that unfortunately happened many years ago, when support for namespaces was dropped from PHP5.0.

azkotoki
+1  A: 

oh my god! why not use comma (,) as namespace separator??!!

we can kill follow syntex: define('a', 'a'); define('b', 'b'); echo a,b;

and assign comma to namespace...

I hope you're kidding!
Tom
+1  A: 

I think backslash looks perfectly fine. I find it easily readable, it's indicative of a depth hierarchy, and the button is right above the return key on most of my keyboards. Given the framing of the choice, I think they picked the best candidate. For those with issues about double backslashing a namespaced class in a string... use single quotes instead of double quotes.

+1  A: 

As a PHP programmer I hate it. We already have ->, so why can't we use namespaces like we do objects? Or, considering there are class methods and variable in PHP5 we could just stick to the already existing :: notation. Or, as already mentioned, commas would work just fine too!

Anything already in use but the irrelevant \!

The Wicked Flea
A: 

Ease of typing on an american keyboard perhaps, but on other keyboards, that's not quite it....

I don't know all the layouts, but on the French ones (big PHP community, after all), a backslash is entered with Alt Gr-8, and I can tell you it's not easy to type !

Bertrand Gorge
It's AltGr+Q *or* Ctrl+Alt+Q on a Croatian keyboard. A real pain in the arse.I don't konw what were they drinking while deciding in favor of \, but I bet it wasn't legal.
dr Hannibal Lecter
As another international, in the Spanish keyboard it's AltGr+`. Studying at a British University, I've realised how tailored programming languages are to the US/British keyboard.
Beau Martínez
+1  A: 

Personally I would prefer the dot ('.'), as in Java and JavaScript.

Voyagerfan5761
Yeah, too bad that's already in use for string concatenation :(
Tom
A: 

I prefer the dot.. it's less ambiguous regardless of its current implementation.

A: 

One word: YUCK.

Clayton
A: 

Ugh. I refuse to use namespaces if the separator remains as a backslash.

A: 

It's plain ugly. I refuse to use namespaces too unless the separator changes! I mean come on, don't tell me nobody raised their hand and said "Guys do you really want this neat feature to look like an ugly windows file path?"

I've been a PHP dev for the past decade and now considering other options because clearly PHP maintainers have no sense of aesthetics!

Zuhair Naqvi