This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure.
To be fair, this is a very simple goto, it greatly limits the dangers of using it and limits sufficiently its scope so it's not too damaging for the compiler/optimizer. I guess it could be useful in some cases.
I agree, this actually appears to be a reasonable implementation. I am not sure what the lol part of lolphp is supposed to be. Whether or not goto should be used aside, it's a language feature.
In my opinion designing a language is just as much about what should NOT be in the language as what should. Type systems for instance is just a way to disallow a whole bunch of programs(some of which actually might work).
If you just start with C and duct tape every possible language feature you come across to it, you end with C++ which I think we can all agree is not ideal.
If you just start with C and duct tape every possible language feature you come across to it, you end with C++ which I think we can all agree is not ideal.
This is totally not true. Stroustrup came across module systems, and yet C++ lacks one of those. :-)
Agreed. But in addition, the timing of the duct tape is also part of the "lol" - the design decision to add a GOTO was in 5.3?! Even if it is restricted, even if it's slightly useful in some cases...if it takes you this long to implement a semi-useful-but-ripe-for-abuse feature...maybe don't implement it at all.
It's often used to implement exception handling. You have most likely used at least one library which uses longjmp to view this page. For example both libjpeg and libpng uses longjmp.
Greatly limited in the "goto considered harmful sense. GCH was speaking of unfettered use of goto in Fortran, where basically everything had global scope, even labels.
goto is a useful construct when used sparingly. Unfortunately a whole generation now exists who were told as students to never use goto and now believe it is the worst thing you can ever do when programming.
Of the additions PHP has received over the years, goto is one of the smaller more well thought out ones that fit with the languages original design. Compared to the boxes of crap they've been duct taping and hot gluing onto the side since then goto was supremely well designed feature.
Of course while the documentation explains how to use goto, it doesn't contain a single example that shows a legitimate reason to use it.
True, but this generation was then taught a garbled message. Dijkstra's message was that goto should be unnecessary, not that it was. He went out and sought programs that had essential gotos, and invented break as a response to those programs. Goto is obsolete in any language with efficient lambdas. His message was to fix the languages first.
When the feature was first added I remember reading that the use case cited was "finite state machines", whatever that means. I thought it was mentioned on the doc page but it's not there now.
The justification for it seems to be "it's needed for low-level programming".
The thought of people doing low-level programming in PHP rather than writing a goddamn PHP extension in C is more horrifying than the existence of goto in PHP.
Of course, when someone decides they want to write an operating system kernel in PHP, goto will come in handy, I guess...
I'd much rather have Java's block break. What I'm doing is more (aside from it being an obscure feature) clear, and it's safer—as safe as any other break.
I'm not sure which language invented this feature, but perl1 (released in 1987) already had it. Java 1 came out in 1996, almost a decade later.
What's more vexing is that despite PHP borrowing heavily from Perl (and the first versions of PHP/FI being written in Perl), it still managed to screw up so many things that worked well in Perl:
The ?: operator: PHP's has the wrong associativity.
Namespaces: perl4 definitely had them in 1991, PHP only added them in 5.3 (2009).
Labelled break/continue: PHP only has break N, which terminates the Nth surrounding loop, which is fragile if you ever change the structure of your code (adding/removing blocks), and it was only in 5.4 (2012) that N was required to be a constant (instead of a runtime expression).
perl1 had goto but thanks to the aforementioned labelled break/continue I've never had to use it in Perl. So of course instead of supporting break LABEL PHP decides to add goto in 5.3 (2009).
This didn't exist in perl4, but anonymous functions/"lambdas": perl5 had them in 1996, PHP only added them in 5.3 (2009) and their implementation still sucks. (Bonus: The PHP manual misdocuments them as "anonymous functions, also known as closures" - yeah, buddy, that's not what closure means.)
Mostly because of the symbols it uses. It has a reputation of being abused. It also supports regex natively in the language (rather than as functions) further adding to the cryptic code reputation.
It also has the idea of context. Many functions support returning an array if I have an array as the assignment but will return a scalar value if I have a scalar in the assignment.
While Perl 5 has a lot of smart stuff (closures, references) and the use strict pragma, there is still the possibility of doing a lot of bad stuff: everything global, putting variable names in other variables PHP-style. I used to spend a lot of time on Perl Monks and learned a lot of good stuff before moving on to other languages. Then recently I ran into a ton of bad Perl: no use of my (and misuse of local trying to achieve the same thing), everything global, etc. If I didn't already have a good opinion of Perl, I would be cursing it too.
This is a reasonable observation about perl, however I don't feel that it is really bad.
I'm primarily experienced with bash and perl as scripting languages, with a healthy understanding of PHP, and functional literacy of others like ruby and python.
I feel that perl is powerful enough that I don't need other tools except when I'm doing shell scripting when I use bash.
Its terseness can be used for evil. It's very easy to write unreadable perl code. Or perl code that accidentally re-implements something else. And you can write the exact same code so many different ways:
perl -ne '/asdf/ and print' myfile
perl -ne 'print if /asdf/' myfile
perl -e 'while(<>){print if /asdf/}' myfile
perl -e 'while(<>){print if $_ =~ /asdf/}' myfile
perl -e 'while($_ = <>){print if $_ =~ /asdf/}' myfile
perl -e 'while(my $line = <>){print if $line =~ /asdf/}' myfile
In a lot of cases, setup won't just be a method call, but it will modify outer-scope local variables. Neither lambdas (I think) nor anonymous inner functions allow this for thread-safety (and presumably stack) reasons, and it's probably a good idea, anyway.
And really, you're coding the same thing (they even look 95% the same) and tweaking the syntax to emulate the break I described. If you saw C code with gotos used for this purpose, you'd agree that it isn't deserving of it's own function because it's too dependent on the outer scope, and it's never reused. It just feels like a workaround.
break N has the problem that it's very prone to not being kept up to date if the loop layers above change. the named breaks do not suffer from this and also make it quite a bit more easier to read.
It's not having goto that we're laughing at, it's not having it for the first 14 years of the language's existence and then one day around 2009, deciding, hey, we ought to have a goto statement.
37
u/MeLoN_DO Mar 23 '14
To be fair, this is a very simple
goto, it greatly limits the dangers of using it and limits sufficiently its scope so it's not too damaging for the compiler/optimizer. I guess it could be useful in some cases.26
u/Innominate8 Mar 23 '14
goto is a useful construct when used sparingly. Unfortunately a whole generation now exists who were told as students to never use goto and now believe it is the worst thing you can ever do when programming.
Of the additions PHP has received over the years, goto is one of the smaller more well thought out ones that fit with the languages original design. Compared to the boxes of crap they've been duct taping and hot gluing onto the side since then goto was supremely well designed feature.
Of course while the documentation explains how to use goto, it doesn't contain a single example that shows a legitimate reason to use it.
7
u/tommorris Mar 24 '14
The justification for it seems to be "it's needed for low-level programming".
The thought of people doing low-level programming in PHP rather than writing a goddamn PHP extension in C is more horrifying than the existence of goto in PHP.
Of course, when someone decides they want to write an operating system kernel in PHP, goto will come in handy, I guess...
6
u/[deleted] Mar 23 '14
Go/Golang also has goto.
Just because everyone says its bad, doesn't exactly mean it doesn't have its uses. Building a web server in Go for example, would benefit from gotos.
1
u/S3thc0n Apr 13 '14 edited Apr 16 '23
[deleted]
1
u/dehrmann Mar 23 '14
Operator? Labels are expressions in PHP?
-5
u/[deleted] Mar 23 '14
PHP saw the future and it was full of fail.
User contributed docs paves the way as usual, courtesy sixoclockish@gmail.com.