Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

PHP Namespaces Controversy

with 31 comments

Java:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Package access:          foo.bar.baz

C#:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Namespace access:        foo.bar.baz

Python:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Module access:           foo.bar.baz

PHP:
Attribute/Method access: $foo->bar
Static method access:    Foo::bar
Namespace access:        C:\foo\bar\baz

What on earth were they thinking?

Written by Federico

October 28, 2008 at 10:22 pm

31 Responses

Subscribe to comments with RSS.

  1. I guess (read as “I hope”) it will be overturned before the 5.3 release, though looking at how wiki says like “it’s the final decision, hoorraay, we’ve decided it finally” I’m not so sure. Anyway, though -> and :: are somewhat plausible, that backslash thingy is just… damn, I don’t have the right words. It’s just mind-blowing. The epic fail.

    But as so far the only people I’ve seen who think positively of it are the php-internals devs themselves – maybe they’ll hear the outcry and get their heads together finally. One can only hope.

    morhekil

    October 28, 2008 at 10:47 pm

  2. While its definitely not pretty, I do believe it was the only feasible solution. PHP deals with (arguably bad) design choices from the past and is paying the price when a new language feature such as namespaces is introduced.

    Evert

    October 28, 2008 at 10:57 pm

  3. Unfortuately, that seems like the direction PHP has gone for a long time: Not with the rest of the languages. :(

    rob zienert

    October 28, 2008 at 11:22 pm

  4. [...] by Scott Gruer | Filed under PHP Federico makes an interesting point about the PHP Namespaces Controversy on his PHP::Impact blog. I never thought about the mess that is PHP until I saw his comparison [...]

  5. [...] Оригинал: PHP Namespaces Controversy [...]

  6. I think the better question is:

    What were they smoking?

    How do I score some?

    Jon Gilkison

    October 29, 2008 at 3:32 am

  7. >C:\foo\bar\baz
    very nice, but incorrectly. need \foo\bar\baz() or \foo\bar::baz()

    AmdY

    October 29, 2008 at 4:33 am

  8. in my opinion almost all characters would have been better than the backslash. it’s not only unesthetic, but in my opinion hard to read (if you read 3rd party source code) and absolutely without any logic.

    sticking with :: would have been the best solution. yesterday i have read an interesting blog entry somewhere, where there was an explenation, how c++ deals with the problem of parsing namespaces and static methods.

    the decision table in the namespace rfc at php website is a big laugh for me. when i read blog entries recently, i too think the only people, who think positive about this, are the php internals. however … i am not really sure, if they really think positive or if it’s just a compromise. but compromises for such important features are crap — better throw namespaces out of 5.3 and think about how to handle them a little longer.

    harald

    October 29, 2008 at 7:45 am

  9. If you read the complete log, you understand it was the only sane choice.

    It goes without saying that it would be nice to resemble other languages but since that has never been te case it can’t be now all of a sudden.

    Maybe someone will ever have the courage to rework a big part (which will break compatibility but creates a brighter future).

    Sam

    October 29, 2008 at 9:41 am

  10. I agree to distinguish static method call and namespace is important. But, why they use “\” instead of others…

    Even “:::” or “@” are better than “\”.

    Mark

    October 29, 2008 at 11:22 am

  11. -> is also used in C+ when dealing with pointers. :: is also used for static calls and member function declaration – can’t see what’s wrong with it and why not stick with :: for namespaces …

    Backslash is just wrong – it’s used for escaping stuff …

    BTM

    October 29, 2008 at 1:41 pm

  12. Unfortunately we can’t use the dot syntax in PHP because it’s reserved for concatenation.

    The big question in my mind is why PHP internals didn’t feel it better to throw a fatal error if you created ambiguous code. For example if a file with class “Foo” and method “bar” was included first, then another file with namespace “Foo” and function “bar” (e.g. both accessed via Foo::bar()), PHP would throw a fatal error on the declaration on namespace Foo function bar.

    The claims of ambiguity between namespaces and classes is moot and frankly beyond stupid given that in every other language I can think of namespaced calls and class calls look identical.

    Daniel

    October 29, 2008 at 2:57 pm

  13. The problem with sharing :: between static class calls and namespace resolution is that in most other languages, classes *are* namespaces.

    That isn’t the case for PHP. Namespaces and classes are entirely detached from one another, which does make :: become possibly ambiguous.

    The obvious solution, of course, is to integrate namespaces and classes, but apparently that’s not “The PHP Way(tm)”.

    They seriously need a syntax reboot. Unfortunately, backwards compatibility wins that argument…

    Charles

    October 29, 2008 at 6:31 pm

  14. that sintax is ugly.
    it seems php internals developer are interested in putting new futures at the language in a “me too” fashion but without lot of consideration of what makes the language clear and beautyfull

    dvsmt

    October 30, 2008 at 12:04 am

  15. One of the criterions from http://wiki.php.net/rfc/namespaceseparator is typo-vulnerability.

    The rating for \ is +1. But this ignores obvious the need to escape \ when namespaces are stored in strings.

    The other ratings are not really understandable.
    Even the selection of rated separators seems to be made with intend to push \ as the result.

    Just onother suggestion: Why not use .. as namespace separator?

    Thomas

    October 30, 2008 at 4:10 pm

  16. Why not use something like :

    Mail:::Client::ReadMailClass->getNewMails()

    Mail::: Being the TOP package (package name)
    Client:: Being the internal package
    ReadMailClass One of the classes contained on the internal package.
    getNewMails A simple (public) method of that class.

    More complicated example :
    Mail:::Server::Storage::Encryption::Sha1->Encrypt()

    Etc

    A simpler (I dont really agree with this “parent methods” of Packages.
    Storage::Encryption->getPossibleEncryptionTypes()

    Clarification : I am speaking of Packages in the same way Flash 9+ code uses them, wish I beleave are quite simple, cute, easy to read and enought for anyones needs. (even tho they use “Mail.Server.Storage” import system, much like C#)

    SilentWarrior

    October 31, 2008 at 7:20 pm

  17. “>>” would have been pretty… too bad it’s a bitwise operator. Hmmm, I’d suggest “->>” but that’s still three chars. I guess that’s why they went with “\”. Well, why not backtick “`” ?

    Heh, I digress. Python, here I come!

    Scott

    November 2, 2008 at 11:18 pm

  18. Hmmm, I wonder what Andi, Rasmus, Zeev and others have to say about this.

    Federico

    November 2, 2008 at 11:39 pm

  19. Oh \ My \ God :: killEm(‘All’);

    Duodraco

    November 4, 2008 at 1:44 pm

  20. I really liked NameSpace1::NameSpece1::Klass->method. But if :: can’t be used why do they think of using \ insted of just : (I think is free) ?

    Radoslav Stankov

    November 6, 2008 at 9:36 am

  21. I like:
    foo:->bar::baz()->exit()
    foo~bar::baz()->exit()

    web20vn

    November 7, 2008 at 6:31 am

  22. [...] grande discução disto tudo foi qual seria o separador do namespace, em muitas linguagens usa-se o “::” também (alias, mesmo separador que o estático), porem com a chamada em maiúscula [...]

    Silici0

    November 7, 2008 at 3:12 pm

  23. [...] ed ai membri delle classi, per PHP invece non sarà così. Riporto un’ironica vignetta di PHPImpact che rende meglio l’idea delle [...]

    PHPnews.it

    November 8, 2008 at 10:38 pm

  24. @web20vn: good idea, but
    http://us.php.net/manual/en/language.operators.bitwise.php
    ~ $a Not Bits that are set in $a are not set, and vice versa.

    I think ~> is better than ~

    foo~>bar::baz()->exit()

    UngDungMau

    November 18, 2008 at 9:38 am

  25. Perl:
    Attribute/Method access: $foo->bar
    Static method access: Foo::bar
    Module access: Foo::bar

    Ben

    November 18, 2008 at 10:47 pm

  26. PHP could easily have “::” for namespaces, if it hadn’t been used for static classes. In this language, it’s possible to use only one operator both for objects and classes without ambiguity:

    $object->field
    $object->objectMethod()
    SomeClass->field
    SomeClass->staticMethod()
    SomeClass->staticMethod()->objectMethod()->objectField

    Zyx

    November 24, 2008 at 3:55 pm

  27. > foo~>bar::baz()->exit()

    That’s the most weird and disturbing thing I’ve seen in my life ;)

    Federico

    December 4, 2008 at 5:26 pm

  28. > foo~>bar::baz()->exit()
    It is truly clear. Foo is a namespace, not class. Bar is a class, yes. Baz is a static method in bar class. Bar method return an instance of the class and call exit method.

    Refresh your life, please ;)

    UngDungMau

    December 26, 2008 at 11:21 am

  29. Well, I agree that \ is ugly. Nobody like it. But almost any other alternatives are not available.

    @ (at) means error suppression.
    : (colon) is reserved for GOTOs and alternative control structures.
    ` (backtick) is for system calls.
    ::: is far too similar to :: and it’s easy to confuse two.
    . (dot) is concatenation.
    ‘Smilies’ things like :> are even uglier and sillier than the backslash.

    The best thing they could do is to throw fatal error when there’s a conflict between static method and a function in the namespace. Or even throw functions in the namespaces away (who wants that non-OOP crap?). I don’t know why they haven’t done that…

    I’ve been waiting for namespaces but now I’m not sure I want to use them, though it’s little bit silly to deny it just because of aesthetics.

    hostit

    December 26, 2008 at 12:49 pm

  30. [...] And, of course, he’s a great guy. Maybe that’s the reason why he was chosen to deliver the bad news to the community. According to some of my colleagues, people where very upset with the new features [...]

    PHP UK Conference 2009 Recap

    February 28, 2009 at 12:34 pm

  31. [...] has been quite a discussion about PHP’s new namespace [...]

    Glen Scott

    June 30, 2009 at 10:01 pm


Leave a Reply