Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

PHP Gets Namespace Separators, With a Twist

Posted by Soulskill on Sun Oct 26, 2008 12:01 PM
from the or-maybe-more-of-a-slant dept.
jeevesbond writes "PHP 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."
+ -
story

Related Stories

This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • by joaommp (685612) on Sunday October 26 2008, @12:04PM (#25518371) Journal

    ... and comming full circle.

      • by Anonymous Coward on Sunday October 26 2008, @12:18PM (#25518491)

        Uhm no. Because the DOS commands used '/' for indicating options as opposed to the '-' of the UNIX world.

      • by joaommp (685612) on Sunday October 26 2008, @12:20PM (#25518509) Journal

        Jeez, take a joke as it is, will you?

          • Re:It's all a joke (Score:4, Insightful)

            by corychristison (951993) on Sunday October 26 2008, @04:20PM (#25520563)

            :: is already used for static methods on classes... it would be harder to implement the differentiation of :: for namespaces and :: for static methods... especially if people started to use classes with the same name as a namespace (which is likely if all modules get their own namespace)

            I actually think that '\' is appealing for what it will be used for. The one thing I first though of was SomeModule\new_object::test_function();

            Wouldn't it try to evaluate the '\n' as a \n new line? I suppose it will be out of the double quite string scope so it could be alright... could get messy if eval()-ing code, though.

            • Re:It's all a joke (Score:5, Informative)

              by Requiem18th (742389) on Sunday October 26 2008, @09:24PM (#25522773)
              And this is (one of) the many reasons PHP sucks:

              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:        foo\bar\baz
              • Re:It's all a joke (Score:5, Insightful)

                by Haeleth (414428) on Monday October 27 2008, @04:34AM (#25524591) Journal

                So spend the $5 and get a new keyboard? Unless your keyboard is physically, permanently attached. Then again we get into the very, very minority.

                "People with laptops" is a very, very tiny minority?

      • Re: (Score:3, Insightful)

        Geez, you have a website that just mirrors Wikipedia and dumps google ads all over it? You must be some kind of a business genius. Particularly since you seem to think that spamming /. will get you clicks. Bandwidth usage and hatred maybe, but clicks, nope.
  • by Anonymous Coward on Sunday October 26 2008, @12:06PM (#25518387)

    PHP 5.3 also adds support for local GOTOs. This langauge is so up with the times.

    • by chrysalis (50680) on Sunday October 26 2008, @03:24PM (#25520043) Homepage

      GOTO is what your CPU is actually doing 80% of the time.
      You can pump up your ego by imagining that using a language without something explicitely called "GOTO" makes your code "up with the time". But what you actually do is nothing but GOTOs, just written in a different manner.

      Ironically, the VM that PHP uses is completely GOTO-based (well, you can pick several methods at compile-time, but GOTO is what a lot of distributions chose because performance is often better than CALL and it's very stable nowadays).

      Oh and even JAVA has GOTO and relies a lot on it. The compiler hides an explicit thing called "GOTO", but what you get after compilation is full of GOTO. And it's actually why apps can actually do something.

      Laughing at "GOTO" is ignorance, or just blind trolling because you read somewhere that BASIC had a "GOTO" keyword. I guess in a few years your children will laugh at those horrible "$", "$this", "->", ":" and "\" symbols, that would remind them the old time of a language called PHP. Though you are proud of them now.

      Using temporary variables like "$should_exit", dummy loops just to "break" at the right place, or named loops to work around "break" that would only exit the first loop is nothing but writing "GOTO" in an obfuscated and inefficient way. "GOTO" is not synonym for "spaghetti code" (the famous keyword always used by people blindly repeating that GOTO is bad).

      Oh and grep for "goto" in your Linux kernel or in any BSD operating system. Wow, tons of them. Really. But I guess this is just because these source codes are shits written by people who can barely write GW-BASIC, and of course none of these operating systems actually work. Glad you are there to help. Teach them how to code, tell them that their code is so passé.

      Or shut up.

      • by lgw (121541) on Sunday October 26 2008, @02:42PM (#25519695) Journal

        Wow, can't type today. Let's try again:

        GOTO remains the best way, in most programming languages, to exit multiple loops, branch to common clean-up code before leaving a function, etc.

        • by coryking (104614) * on Sunday October 26 2008, @03:01PM (#25519849) Homepage Journal

          Modern languages have "exit for" or "break" to bail out of a loop.

          If you have a triple nested loop in the same function, you should refactor the code and move the inner loops into another function.

          What do you mean by "Clean Up Code"? If you have so many branches in a single function, again, refactor the code and split them into multiple functions.

          See also: Code Complete [cc2e.com]

          • by lgw (121541) on Sunday October 26 2008, @03:43PM (#25520205) Journal

            Yes, for school problems, refactoring the code to make the inner loop a function is great. For real-world legacy crap code, it's often impractical. You might have 15 variables that would need to be passed into that inner loop, and you might have shop standards preventing you from passing some of those types into functions, etc, plus your boss (often rightly) object to you "refactoring" code that works today. "Refactoring" is a charmingly naive expectation for legacy crap code to begin with. And if you're not working with legacy crap code today, consider yourself lucky: you don't know how good you have it.

            And by "clean-up code" I mean: you allocate resources at the top of a function, so they must be cleaned up at the bottom of the function (and no sneaky returning from the middle of the function). That code at the bottom of the function is "clean-up code". If you have 15 possible errors in your function, you either have some unreadable mess with 15 nested if statements that hide a perfectly straightforward logic flow, *or* you branch to the clean-up code in each of the 15 error cases, making the actual logic of the function obvious.

            Of course, most coders are simply incompetant, don't even bother to check for errors, and certainly don't ensure that every resource allocated at the top is easily visually identifyable as being freed at the bottom, which is why there are so many job maintaining legacy crap code (and why Java exists in the first place).

            And of course there are languages in which "goto" is pronounced "throw" and all the clean-up happens automatically, but mostly it's inventory and payroll databases that get coded in such languages: give me legacy crap code that does something *interesting* any day! I will forever cherish the one job I had in which C++ was used correctly (not the usual typing C into a cpp file) and goto was really unnecessary, but I don't expect lighting to strike twice in my career.

            Blah, blah, Code Complete. Some of us were doing that stuff correctly long before Steve McConnell (and I'd hardly cite Microsoft as an example of a stable, secure, maintainable code base!).

  • by jspenguin1 (883588) <jspenguin@gmail.com> on Sunday October 26 2008, @12:06PM (#25518391) Homepage
    I couldn\'t read the summary because it had an unterminated string literal.
  • by Anonymous Coward on Sunday October 26 2008, @12:09PM (#25518409)

    It'll be /, just to keep things interesting.

  • by A beautiful mind (821714) on Sunday October 26 2008, @12:13PM (#25518443)
    ...to make PHP the most retarded computer programming language on the planet.
    • by FooAtWFU (699187) on Sunday October 26 2008, @12:16PM (#25518457) Homepage
      Oh, they've been at it for a while now [www.tnx.nl] ;)
    • by mangu (126918) on Sunday October 26 2008, @12:20PM (#25518505)

      I once did a lot of work on PHP. Today, when people ask me for upgrades I just migrate it to Python.

      This unfortunate choice of the escape character for namespace separator is stupid, but seems almost irrelevant to me. How many nails do they need in the PHP coffin to bury it?

      • by Dragonslicer (991472) on Sunday October 26 2008, @12:37PM (#25518627)
        PHP is far from dead. PHP5, with support for real OO, was a huge improvement. There's been a lot of hard work put in to PHP in the last few years to make it a much more viable modern programming language.

        Then I see people suggesting \ for a namespace separator, and I wonder what happened to all the people that put so much work into making PHP5 good, and why we can't get them back.
        • Re: (Score:3, Insightful)

          I wonder what happened to all the people that put so much work into making PHP5 good, and why we can't get them back.

          The last one was seen downloading a Ruby On Rails development environment.

        • by chrysalis (50680) on Sunday October 26 2008, @03:42PM (#25520199) Homepage

          PHP ? Real OO? Thanks for the great joke.
          How can I add methods to Number? Ehm you know, the class used for numbers... In order to write 3.times() for instance... ah, it doesn't exist? Ok, so how can add methods to strings? Impossible, strings aren't objects either?

          Stop kidding.

          Why do PHP programmers use classes for? Just to avoid collisions between two functions with the same name when files are included. Really... very few PHP code instanciates more than one object per class.

          Introduction of namespaces might limit this.

          But there's something else that PHP miss: a "static" keyword.

          Guess how very large source code like OS kernels or demos have been built in C or assembler, without namespaces, without classes, without symbols like \, but without coders constantly fighting about names collisions?

          The reason is file-local symbols, ie. the static keyword in C (and local symbols in assembler). Only export (ie. make non-static) what you need to use in other files. As a bonus, it helps the compiler in order to optimize the code.

  • yet another wtf (Score:3, Interesting)

    by larry bagina (561269) on Sunday October 26 2008, @12:18PM (#25518489) Journal

    The rfc [php.net] claims that typing "**" is easier than typing "%%" or "^^".

  • Backslash! (Score:4, Funny)

    by SEWilco (27983) on Sunday October 26 2008, @12:24PM (#25518537) Homepage Journal
    Just make sure they name it a backslash in the documentation, not a slash.
  • ... to cause for windows servers...

    imagine what directories will be deleted due to a typo!

  • other issues (Score:5, Insightful)

    by adamruck (638131) on Sunday October 26 2008, @12:34PM (#25518613)

    Maybe they could starting fixing the noun-verb vs verb-noun problems instead.

  • The number of days that an old, crusty Perl developer can laugh at another language are few and far between.

    Thank you, PHP.
  • by Anonymous Coward on Sunday October 26 2008, @12:54PM (#25518761)

    How is it possible for even American developers to be this clueless. Which characters are convenient to type depends entirely on the keyboard layout that is used. Case in point, $ is insanely painful to type on Scandinavian layout.

    If your choice of characters used in your programming language is affected by how easy/hard it is to parse the code, you probably shouldn't be involved in developing a programming language.

  • by janwedekind (778872) on Sunday October 26 2008, @01:04PM (#25518833) Homepage
    Looking at the IRC discussion [php.net] it seems that they didn't have much of a choice.
  • Other suggestions (Score:3, Interesting)

    by lepidosteus (1102443) <lepidosteus@@@gmail...com> on Sunday October 26 2008, @01:26PM (#25519029)

    For additionnal fun, read this: http://wiki.php.net/rfc/namespaceseparator [php.net]

    Looks like they considered stuff like :> and :) as separators for namespace. Seriously.
    Also, they don't give any malus for tybe-ability to \ while on most european keyboards it's a lot harder than any other suggested separator. Way to go !

  • by sfjoe (470510) on Sunday October 26 2008, @02:27PM (#25519549)

    Since PHP is open source, someone will make a fork with a different separator and the dumber of the two choices will wither away.

    • Slashcode runs off of Perl. They must hates it with a passion.

      [sic]

    • Re:what wrong with (Score:5, Insightful)

      by anotherone (132088) on Sunday October 26 2008, @12:19PM (#25518501)

      PHP uses the . as the concatenation operator. PHP does not support operator overloading...

        • Re:what wrong with (Score:5, Insightful)

          by moderatorrater (1095745) on Sunday October 26 2008, @01:25PM (#25519017)
          The whole purpose behind using '.' as string concatenation instead of '+' is that it eliminates ambiguity. What you're suggesting throws the ambiguity back in. Remember, if it's more complex for the parser to understand, it's more complex for a human to understand. As a programmer who moves between PHP and Javascript a lot, I can tell you that I miss being able to use a dot for objects when I'm in PHP, but the ambiguity in string concatenation/addition in javascript is an order of magnitude more annoying.

          I suspect they're doing the same thing with namespaces. The backslash isn't used for anything except escaping strings, and I doubt that's going to add any ambiguity at all. There are a lot of problems with PHP, and it's well worth your ridicule, but making sure that separate operations have separate operators isn't one of those problems.
            • Re:what wrong with (Score:5, Informative)

              by moderatorrater (1095745) on Sunday October 26 2008, @02:37PM (#25519627)
              As I remember, everything more complex than just outputting the value of the variable (ie calling a method, accessing a property, etc) requires you to use brackets inside of the string. Namespaces would work the same way without adding any complexity that wasn't already there.
    • Re:WTF? (Score:5, Interesting)

      by ThePhilips (752041) on Sunday October 26 2008, @12:50PM (#25518723) Homepage Journal

      You should say "thanks" they haven't chosen something else. e.g Jam (build system; make analog) uses "!" as a "platform neutral" path separator. During evaluations for new build system I joked that I oppose jam since we do not need a "platform neutral" system - we need one for *nix and cygwin. To my surprise many supported me.

      I think their decision to use '\' is very very dumb one.

      I'm still huge fan of Objective-C in that aspect. Unlike C++, which tried to marry C and objects, ObjC took more pragmatic approach: C constructs remains C constructs and object oriented constructs got new distinctive syntax so that you can never mix up what code you are looking at.

      In that aspect, I think PHP folks would regret their decision in future: '\' isn't distinctive enough and they would need to introduce more silly syntax hacks when extending language further.

    • by coryking (104614) * on Sunday October 26 2008, @01:03PM (#25518825) Homepage Journal

      Except you just made a typo. It is "\" instead.

      Either way, most languages use either "." or "::" for namespaces

      # perl looks like
      use My::CPAN::Module qw();
      my $instance = My::CPAN::Module->new("junk");

      # c# looks like
      using System.Windows.Controls;

      System.Windows.Controls.ListBox box = new System.Windows.Controls.ListBox();

      # c++ looks like (I think)
      namespace Blah::Blah;

      # php will now look like
      $object_instance = new My\PEAR\Module("myvar");

      I'll leave the "looks" of PHP's method to the reader.

    • by SoapBox17 (1020345) on Sunday October 26 2008, @01:11PM (#25518883) Homepage
      Since PHP is a weakly typed language, using + for string concatenation would introduce a number of problems. + is used numeric addition, and thus automatically converts the operands to numbers before adding them.

      So using + for string concat too would be basically impossible... When would you decide to concat the operands, and when would they be added? If you base it on the results of the string to number conversion, you get situations where the same line of code sometimes adds numbers and other times concatenates strings, or where it is impossible to concat two strings which contain only digits.
    • by SUB7IME (604466) on Sunday October 26 2008, @01:12PM (#25518899)

      Scripting languages are for those of a weak mind and poor technical skills and the singular lack of the ability to plan a system out before you write one line of code.

      Or for projects that need to be compiled at runtime. But, nice magnanimity.

    • At least in a sense. You can map a .NET namespace to an XML namespace. Say you have namespace that is:

      Shados.Awesome.Controls

      You can map that into:

      http://www.shados.com/controls

      In c# you'd plunk this into your AssemblyInfo.cs file:
      [XmlnsDefinition("Shadows.Awesome.Controls","http://www.shados.com/controls")]
      And thus add it to your XAML code:


      <UserControl xmlns="http://www.microsoft.com/xml/something"
      xmlns:shadow="http://www.shados.com/controls">
      <shadow:AwesomeControl x:Name="myControl" param="aPar

    • by mysidia (191772) on Sunday October 26 2008, @01:20PM (#25518959)

      The problem is not merely that it is different.

      The problem is they chose the ESCAPE character as a namespace separator.

      This is even worse than using $ as the namespace separator.

      Because of the problems it causes syntax highlighters, the problem it causes to programmer sanity when storing identifier names in a string.

      The problem it causes when searching through and sanitizing code.

      For example, since \ has a special meaning in the context of a regular expression, searching and replacing using regular expressions just got very painful.

      Copy and paste no longer works for searching and substituting.

      Refactoring just became a major bitch.

      • by coryking (104614) * on Sunday October 26 2008, @01:33PM (#25519121) Homepage Journal

        Now do you have to escape your namespaces before passing them through eval?

        eval("$instance = new My\\Super\\Class(\"blah\"););

        Since they now are using the escape character for namespaces, I wonder what kinds of security implications this might have? What happens when a PHP program for some reason evals() some user input that doesn't properly escape the namespaces?