8 ms·
I'll admit, I loled. What is the reasoning behind goto in PHP though? I know php well enough, and I can't think of a time or place I've ever said: "I really wi
by run4yourlives 17y ago
I'll admit, I loled.
What is the reasoning behind goto in PHP though? I know php well enough, and I can't think of a time or place I've ever said: "I really wish I had a goto here!"
- roder 17y agoSeems like a legitimate bug submission to me.
- wvenable 17y agoPHP is ubiquitous, and now I can be more easily targeted by languages that can compile to PHP. They must have had a use-case in mind when adding it, because it's a lot of work for nothing otherwise.
- mnemonicsloth 17y agoThe standard Acceptable Goto is used to throw control between levels of a multiply-nested loop. See also: call/cc for managing control flow in a tangle of mutually recursive functions.
- blasdel 17y agoBut it's not "a lot of work" to implement at all! All of the standard 'structured programming' constructs (if, while, for, switch, etc.) are just wrappers around conditional jumps in nearly every imperative language. switch even uses labels for fuck's sake!
- eru 17y agoI'll give you a point if you become more polite.
- reduxredacted 17y agoIf you've ever been able to think of a time or a place that you've said "I really wish I had a goto here!", it might be time for a rewrite or a new job. Though, admittedly, I don't think I've ever looked at a piece of code I've written that was more than six months old without thinking "who the hell did this and why did they want to torture me?"
- scott_s 17y agoSee http://news.ycombinator.com/item?id=650027 http://news.ycombinator.com/item?id=650027
- run4yourlives 17y agoIf you are raising an error/exception, shouldn't you be doing that explicitly, instead of just outputting catch all error handling at the end of the routine? Otherwise, we may as well just do: "on error resume next :-)"
- scott_s 17y agoThis is C, in the kernel. You can't "raise an exception." The gotos are necessary because kernel work requires allocating resources. If an error is hit, the function needs to exit, but it also needs to release its resources correctly. But, these resources need to be freed upon normal completion as well. Hence, gotos. You should probably read some of the code before saying it's bad. See, for example, the function that implements a memory map: http://miller.cs.wm.edu/lxr3.linux/http/source/mm/mmap.c?v=2.6.11.12#L864 http://miller.cs.wm.edu/lxr3.linux/http/source/mm/mmap.c?v=2...
- run4yourlives 17y agoWell, fair enough. My ignorance of C kernel programming is on display, but I'm pretty sure the topic was why we'd need goto's in PHP. Again, I'm not seeing any specific need.
- shiro 17y agoGoto is pretty handy if you're not writing in the language but writing a translator to the language instead. That is, when some corporate politics demand the product must be in PHP but you feel you start losing your mind writing in it... well, write in your favorite language and compile it to PHP!
- 17y ago
- mrshoe 17y agoCommonly cited use cases include: 1. Break out of nested loops. 2. Jump to cleanup code at the end of a function instead of an early return. I never use goto, but I think the usual knee jerk reaction to it is sometimes unwarranted.
- daremon 17y agoAbout (1), what's wrong with break? (it also accepts a level e.g. break 3)
- WilliamLP 17y agoBreak 3 is really unclear about where it's going to take you, especially if the code is poorly formatted, and doubly especially if you decide later that you want change the code and put another loop in it.
- tilly 17y agoIf you have them in your language the common better solutions to those use cases include: 1. Labeled loop control 2. Exceptions PHP has #2 and a bad form of #1. In PHP you get to say how many loops to break out of, but not the name of the loop to go to. This makes code less self-documenting and can be fragile when editing code. Other uses of goto for which substitutes are harder to find are to to make your language a more convenient compilation target, and when interacting with other control structures not under your control. These uses are VERY rare. In a decade of professional Perl programming I have seen each used twice. None of the examples were in any code base I worked with. Two were in the Perl core, the first is that the s2p utility emits gotos to emulate branches in sed, and the second is that the original Switch.pm used a goto to break out of any loops that it happened to interact with. (That one was a computed goto! After all he didn't know how this switch might interact with other switches.) I would be willing to use goto for nested loops in PHP, but only because PHP does not offer labeled loop control. While I like to believe that I understand when to use goto for other reasons, I doubt I'll ever encounter a serious need to do that in any language.
- jerf 17y ago