Page 2 of 2

Re: Goto in PHP

Posted: Fri Jan 09, 2009 6:17 pm
by josh
I did not realize that... i feel kinda stupid now

Re: Goto in PHP

Posted: Fri Jan 09, 2009 6:43 pm
by Christopher
pytrin wrote:In my opinion, a GOTO in the traditional sense is reuse without scope. This is dangerous, like using a global, but can be used to solve difficult problems very easily (but not elegantly - in my opinion). I liked though the loop example they had in that discussion volomike linked to, that could be a nice addition.
Well then you would also have to argue that switch() has the same problem -- it was structured programming's last attempt to hiding everything but the last 1% of goto's use. All of the structured constructs are gotos -- but with scope as you rightly point out.

The one well known exception to the "goto bad" rule is parsers (and generally state machines). If you have done much work on parsers you probably know that there circumstances where not using goto produces much worse code that using it...

Re: Goto in PHP

Posted: Fri Jan 09, 2009 6:47 pm
by Eran
I imagine that on a lower level GOTO are a must, since somewhere along the line you have to declare what functions are. But PHP is a much higher level language, where sufficiently advanced scoped structures are available, so I can't really see a good use for a GOTO. If someone has a good example, I would love to see it.

Re: Goto in PHP

Posted: Fri Jan 09, 2009 10:57 pm
by Syntac
I've never ever found a need for goto when writing PHP. I doubt that's going to change.

Re: Goto in PHP

Posted: Sat Jan 10, 2009 12:18 am
by volomike
arborint wrote:you would also have to argue that switch() has the same problem
Is it okay to use switch() instead of if/else if/else control structures? I use switch() all the time and I like how I can layer things like so:

Code: Select all

switch(Controller::Action) {
  case '/register-candidate':
  case '/register-employer':
    Controller::Dispatch('Registrations/Register');
    break;
  case '/':
    Controller::Dispatch('Home/Login');
    break;
  default:
    Controller::Dispatch(404);
    break;
}

Re: Goto in PHP

Posted: Sat Jan 10, 2009 1:40 am
by Christopher
pytrin wrote:If someone has a good example, I would love to see it.
The one example I gave is parsers. That is usually given as an a good case for inclusion for goto.
Syntac wrote:I've never ever found a need for goto when writing PHP. I doubt that's going to change.
But your past experience is not the basis for designing language features.
volomike wrote:Is it okay to use switch() instead of if/else if/else control structures? I use switch() all the time and I like how I can layer things like so:
I use switch occasionally as well. I was just pointing out that it is really just a structured goto -- and a pretty blatant one at that. That is typically why programmers tend to intuitively shy away from using switch().

And to be clear, the language feature being added is not goto but break to label. I think it is a nice compromise addition to the language because, as I said about, using this kind of goto in looping state machines, such as parsers, can turn a horrible mess of structured constructs into much cleaner, more understandable code. It may be a rare use case but it is a real one.

Re: Goto in PHP

Posted: Sat Jan 10, 2009 1:47 am
by Eran
The one example I gave is parsers. That is usually given as an a good case for inclusion for goto.
I asked for an example in PHP. As far as I know, PHP is not meant to build language parsers.
I was just pointing out that it is really just a structured goto
The main difference between a switch or if/else and a GOTO, is that those structures can't go back in the code and violate the expected direction of statement evaluation flow - from the first row to the last row. To me that is like going back in time (ie, forbidden. did you see back to the future? ;) ). The most they can do is skip forward which is needed for optional parts of a program, and they do declare a visible strucutre for that part. A goto is much harder to track logically.

Re: Goto in PHP

Posted: Sat Jan 10, 2009 2:01 am
by Christopher
pytrin wrote:I asked for an example in PHP.
How could one be given -- there is no goto in PHP! The example I gave is one for why goto is included in general in languages.
pytrin wrote:As far as I know, PHP is not meant to build language parsers.
Again, you may not want to write them. There are certainly parsers for YAML, JSON, etc. done in PHP. The question of whether you or I would write a parser in PHP, or use goto, is separate issue from whether anyone might want to write a parser or whether goto should be included in the language.

Re: Goto in PHP

Posted: Sat Jan 10, 2009 2:07 am
by Eran
How could one be given -- there is no goto in PHP! The example I gave is one for why goto is included in general in languages.
I meant a good example where a GOTO might simplify something in PHP...
Again, you may not want to write them
I'm not talking about me wanting or not to do something, I'm talking about appropriateness of the feature to the language. PHP was not meant to be a general purpose language, though it can do many things. Asking for a potentially troublesome to implement features the language is not intended for is not a convincing case.

And I thought you meant programming languages parsers, not data structures like JSON or YAML. Maybe you could give me a good example with one of those?

Re: Goto in PHP

Posted: Sat Jan 10, 2009 2:29 am
by Christopher
As I said -- parsers. Specifically looping state machines. The reason goto is useful in these is that you often end up with complex if() and switch() statements to deal with skipping over sections of code in the loop given the current state. The use of gotos cleans up this code -- in the opinion of some, though others may say it does not. If you have ever looked at the output of yacc or lex you would know what I mean. As I have said, it is not a very common case. But as I have said, it is one real case that obviously a number of PHP programmers have voiced an interest in. It is quite possible that even with these examples you may still disagree.

And I seriously doubt that many PHP programmers are going to hurt themselves on break to label...

Re: Goto in PHP

Posted: Sat Jan 10, 2009 2:31 am
by Eran
I actually said that the example they gave (break to label) is a nice one :)

Re: Goto in PHP

Posted: Sat Jan 10, 2009 2:49 am
by Christopher
Yikes! You mean after all of that we agree that goto in general is a bad idea but in certain narrow cases that something like break to label might be handy!!! :) :drunk:

Re: Goto in PHP

Posted: Sat Jan 10, 2009 4:07 am
by Eran
:P