Slashdot is powered by your submissions, so send in your scoop

 



Forgot your password?
typodupeerror
×
PHP Programming IT Technology

PHP Gets Namespace Separators, With a Twist 523

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."
This discussion has been archived. No new comments can be posted.

PHP Gets Namespace Separators, With a Twist

Comments Filter:
  • by Anonymous Coward on Sunday October 26, 2008 @01:06PM (#25518387)

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

  • WTF? (Score:1, Insightful)

    by Anonymous Coward on Sunday October 26, 2008 @01:08PM (#25518403)

    Being different to be different, aren't we?

  • That's because.. (Score:1, Insightful)

    by Anonymous Coward on Sunday October 26, 2008 @01:08PM (#25518405)

    There's obvious drawbacks for using a period (.) as a string concatenator, it's easy to put strings together since typing a period is a lot easier than a plus (+), but this means you can't use a period for namespaces as many OO coders are used to..

  • by A beautiful mind ( 821714 ) on Sunday October 26, 2008 @01:13PM (#25518443)
    ...to make PHP the most retarded computer programming language on the planet.
  • by DontLickJesus ( 1141027 ) on Sunday October 26, 2008 @01:18PM (#25518495) Homepage Journal
    As a developer who primarily works with C#/.Net, with a little PHP on the side, IMO this addition is long overdue. It would be nice to have a more standard separator, but when I RTFA they seem to have a just reason for it, and it's using semantics that PHP coders are used to. I will happily adjust myself in this ONE manor in order to reap the MANY benefits of namespaces.

    Just my 2 cents.
  • Re:what wrong with (Score:5, Insightful)

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

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

  • by mangu ( 126918 ) on Sunday October 26, 2008 @01: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?

  • Re:yet another wtf (Score:1, Insightful)

    by Anonymous Coward on Sunday October 26, 2008 @01:26PM (#25518557)

    All I can say is that "\" is a key combination of a right hand key with AltGr on a German keyboard. How is that easy to type? "::" is Shift-. on all keyboards that I know of.

  • other issues (Score:5, Insightful)

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

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

  • by Shados ( 741919 ) on Sunday October 26, 2008 @01:35PM (#25518617)

    Thats all good. I personally feel its just easier to avoid PHP altogether and not have to adjust to all of the language's quirks for little to no benefits from other offerings. Simpler that way.

  • by Zontar The Mindless ( 9002 ) <plasticfish.info@ g m a il.com> on Sunday October 26, 2008 @01:48PM (#25518695) Homepage

    It's not just developers, it's things like IDEs which highlight anything starting with '\' as an escape sequence.

    Which - since nearly every other common programming or scripting language uses '\' to mark the beginning of an escape sequence - is something you might reasonably expect.

    Come to think of it, so does PHP... ooops...

  • by mangu ( 126918 ) on Sunday October 26, 2008 @01:49PM (#25518703)

    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 Anonymous Coward on Sunday October 26, 2008 @01: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.

  • Re:WTF? (Score:5, Insightful)

    by caluml ( 551744 ) <slashdot@spamgoe ... minus herbivore> on Sunday October 26, 2008 @02:02PM (#25518819) Homepage

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

    You've summed up my opinion concisely. That is *truly* retarded to use the (almost?) universal escape character for another reason. Almost as retarded as Microsoft going with \ for a directory separator.

  • by SoapBox17 ( 1020345 ) on Sunday October 26, 2008 @02: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 @02: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.

  • by The Bender ( 801382 ) on Sunday October 26, 2008 @02:19PM (#25518955) Homepage
    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.
  • Re:what wrong with (Score:5, Insightful)

    by moderatorrater ( 1095745 ) on Sunday October 26, 2008 @02: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.
  • That link compares PHP to Perl.

    And pretty soundly shows Perl to be as good as or better than PHP, in some fundamental ways.

    I spit on Perl and so does everybody else I know.

    You apparently don't know a lot of people who actually understand Perl.

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

    Calling a variable "myInt" doesn't make it an integer -- it was a float from the second you added a dot.
    First, duh. Second, are you sure it will return "hello1.0" and not "hello1.00" or "hello1"? I'm not, I could test to find out, but that makes my point. It adds ambeguity to the language and makes me think about something I really should be thinking about. To use your example, what does the following result in?


    function blah(arg1) {
            var blah = "10" + arg1 + 5 + "hi";
    }

    alert(blah("hi"));
    alert(blah(1));
    alert(blah("1"));

    My example isn't as contrived as yours. What if blah() was in some other JS file. It could be easy to trip the function up based on what you pass in.

    Using "." for string concatenation at least gives both you, the code reader, and the compiler a hint at what you mean. That way the compiler can barf if you do something silly like concatenate an integer with a string. Using "+" for both addition and concatenation just makes more work for both parties.

    In any case, I don't find concatenation nearly as useful as interpolation, most of the time. In Ruby, at least, interpolation is known to execute faster than concatenation. But that doesn't help you if you're using JavaScript.

    I agree with this statement. I much prefer how Ruby and Perl do it... just toss the variables into your string and it will interpolate them.

    BTW, I'm not too familiar with Ruby. Does it pull the same trick Perl does and use a different operator for string comparison?


    if($myStr eq "hi") { print "hi";}

  • by lgw ( 121541 ) on Sunday October 26, 2008 @03: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.

  • Re:Phalanger (Score:3, Insightful)

    by shutdown -p now ( 807394 ) on Sunday October 26, 2008 @03:44PM (#25519719) Journal

    Allowing unicode characters in identifiers is SICK and not to be encouraged by any programming language or any compiler for any reason, as the issues are even more widespread than with this PHP issue.

    Why, exactly? English is not the only language spoken in the world, you know, and programmers in many countries feel more at ease naming identifiers using their native language rather than trying to come up with an English translation with their limited knowledge of English. In fact, when people who don't know English well are forced to use English names in identifies, the mistranslations that result can often be very confusing - take it from someone who has to deal with such code daily. And most languages in the world require at least a few letters not in ASCII.

    By the way, have you seen Fortress programming language? You know, the one that not only allows Unicode, but uses its fully in the core library, including all standard mathematical operators (with their normal meaning). It is surprisingly readable.

    Unicode is used throughout for several years now. Programming languages are pretty much the only place where ASCII still holds, but ever since Java, this has been relaxing more and more - and rightly so. Look back 30 years - do you remember all that code written in all-uppercase because many computers of that time simply didn't have lowercase letters? And it's all gone now, because there's no longer any need to account for such machines - they don't exist anymore. Restrictions on plain ASCII only are heading the same way, and for the same reasons. All editors support Unicode now, so it is used already, and it will be used more and more.

  • by coryking ( 104614 ) * on Sunday October 26, 2008 @04: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 Tom ( 822 ) on Sunday October 26, 2008 @04:05PM (#25519883) Homepage Journal

    This line from the IRC discussion says it all:

    [16:24:51] bjori: switch to US layout ;)

  • by chrysalis ( 50680 ) on Sunday October 26, 2008 @04: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 chrysalis ( 50680 ) on Sunday October 26, 2008 @04: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.

  • by lgw ( 121541 ) on Sunday October 26, 2008 @04: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 billcopc ( 196330 ) <vrillco@yahoo.com> on Sunday October 26, 2008 @04:46PM (#25520245) Homepage

    So how do you like scamming your ads off of Wikipedia's content ?

    Just when I thought the human race couldn't get any more pathetic, I get proven wrong.

  • Re:WTF? (Score:2, Insightful)

    by BollocksToThis ( 595411 ) on Sunday October 26, 2008 @04:47PM (#25520261) Journal

    Given that / support has only been present in recent Windows versions, I have to say
      [citation sorely needed]

  • by prockcore ( 543967 ) on Sunday October 26, 2008 @04:47PM (#25520271)

    You'd have to do that anyway, because you used double quotes, "$instance" is going to get evaluated and your eval will fail anyway.

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

    by corychristison ( 951993 ) on Sunday October 26, 2008 @05: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.

  • by hawk ( 1151 ) <hawk@eyry.org> on Sunday October 26, 2008 @05:37PM (#25520715) Journal

    Although rare, there are times when GOTO is the cleanest way out.

    I recall being stunned to run across one of them writing the code for my dissertation, and noting that I was going through a *lot* of code to avoid a single, simple GOTO. Yes, I could have avoided it, but under the circumstances, the GOTO was much cleaner.

    Oddly, I don't remember the circumstances; just my sheer amazement that such circumstances actually existed.

    hawk

  • by Master of Transhuman ( 597628 ) on Sunday October 26, 2008 @06:03PM (#25520905) Homepage

    with 4GB RAM machines with TB hard drives - and we're still worrying about "the number of characters".

    Oh, please. Fucking nerds.

    For the last forty years, we've been constrained by one pointless limitation after another, not to mention the complete inability of a PC to discern what is an identifier and what is command syntax if it has fucking SPACES in it.

    Get your heads out of your asses.

    And learn to type.

    "Number of characters" - Jesus Baron von Christ!

  • by glwtta ( 532858 ) on Sunday October 26, 2008 @06:22PM (#25521069) Homepage
    Yeah, and if you are running PHP on a Windows server, do you really need any more trouble?
  • by Thuktun ( 221615 ) on Sunday October 26, 2008 @08:48PM (#25522143) Journal

    GOTO is what your CPU is actually doing 80% of the time.

    And your car's engine spends all of its time repeatedly causing small explosions with volatile petroleum.

    The driver is generally recommended to let the engine do this and not try to intervene or do it themselves.

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

    by Haeleth ( 414428 ) on Monday October 27, 2008 @05: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?

HELP!!!! I'm being held prisoner in /usr/games/lib!

Working...