Goto for PHP: Heaven or hell? - Blog - Open Source - schlitt.info

schlitt.info - php, photography and private stuff

Goto for PHP: Heaven or hell?

Reading all those mails from the past weeks, I stumbled over several dealing with a patch for PHP provided by Sara Goleman, which provides "goto" statements for PHP. There seem to be a lot of different opinions on that topic, in my eyes mainly depending on the code camp the specific person comes from. I wanna pick up some of the statements I found against goto and comment on them.

Goto is evil

I remember doing Basic, ages ago, which did not know things like procedures and function (not even thinking of OOP). Doing goto (based on the line numbers) was the only way to go. Yeah, that were my first experiences with programming. The code looked horrible and was almost unmaintanable... (think of renumbering the lines, when you wanna insert code). Then we started with Pascal in school and goto's were the evil on earth...

Can you see the problem, why goto has been considered evil? In my eyes, it's not goto itself, but the absence of any other possibillity. Imagine we've had functions these times, I guess that goto would have been used in a much other way.

Goto conflicts with the concept of OOP

True, goto conflicts with the conflict object object orientation in some way. Even true, PHP just invented a new OO model. Even more true PHP does follow the trend of OOP with that. But does goto really conflict with that happenings?

Not in my eyes. What I always loved in PHP was the possibility to choose how you would like to solve a problem. OOP is nice, but it's overhead in a huge bunch of cases. Functions are nice, but even they are overhead in some place (think of a simple shellscript). Having the choice of which approach to follow with the next project is a freedom I enjoy. Even better is the possibility too combine. So, where is the bad side of adding another technique of coding and give people the choice? Does one really think, that people will fall into Spagetthi code again these days, when having the possibility?

Switch has the same concept

True in some ways, but using a switch instead of goto looks a) really ugly and b) allows you just a single jump in PHP. The sense of switch is to react on a given value in one of several defined ways. Gotos allow you to jump between multiple marks more often.

Everything you can do with goto can be modeled by other constructs

This is no true. Some cases (e.g. issues in parsing and some nice looking error handling techniques) can not be modeled exactly as goto can do. Even if, the constructs for that are mostly more complex, ugly and unmainanable as the goto solution would have been.

In general I think Sara's patch cannot be bad at all. Giving the to the user looks pretty senseful in my eyes and the security restrictions for goto sound sensible for me:

  • No jumping between functioncs

  • No jumping into/out-of the global scope

  • No jumping from any code in one file to code in another file (even if it's global scope on both ends)

Although I don't understand the last point.

Anyway: Thanks to Sara for the cool patch and hope that goto will find it's way into PHP5.

If you liked this blog post or learned something, please consider using flattr to contribute back: .

Trackbacks

Comments

Add new comment

Fields with bold names are mandatory.