What are the valid use cases of goto in PHP?

Deadly 提交于 2019-12-30 07:59:10

问题


I know, there are other questions about the goto statement introduced in PHP 5.3.

But I couldn't find any decent answer in there, all were of the type "last resort", "xkcd", "evil", "bad", "EVIL!!!". But no valid example. Only statements that there aren't any uses. Or statements that there are some rare use cases (again, without examples).

So, the question is: "What are the valid use cases of goto in PHP?". Answers for "Is goto evil?" are not welcome and will get downvoted. Thanks :)

Or does somebody have a link to an RFC where the decision is explained - I couldn't find one.


回答1:


One possible use (that can however be be implemented using other means) is implementation of a finite state machine.




回答2:


See PHP Architect - GOTO in PHP 5.3: is it Really That Evil?

This article gives a short overview of why GOTO can be useful, why we have it in PHP (since 2004) and why it was/is controversial. The general answer seems to be: GOTO is mostly useless and should be avoided unless in very narrow application spaces (like building a compiler):

One thing is certain: there are some application in which a good developer can put GOTO to good use to produce code that is simpler and more efficient, but they are probably not the kind of mainstream programs that your average developer will be writing; therefore, there is a legitimate concern here that inexperienced programmers will use GOTO to the general detriment of their code.

Also see some of the original newslist discussion/controversy mentioned in the article:

  • http://www.mail-archive.com/internals@lists.php.net/msg10274.html
  • http://www.mail-archive.com/internals@lists.php.net/msg20664.html

Also see this thorough language agnostic discussion about GOTO considered harmful at C2 Wiki




回答3:


One case where I use it is when I have multi-level checking to do and don't want to add extra code when I could just jump to the relevant section after my checks.

For example, there's this system that checks the status of various machines in a warehouse, and fails the warehouse as a whole and sends out an alert if any one part of any one system is dead, in pseudo-code it goes like this:

if(hsensor[status] != OK) {
    alert = Humidity sensor hsensor[fail_id] failed with error hsensor[fail_error]
    goto sendAlert;
}

if(cosensor[status] != OK) {
    alert = Co2 sensor cosensor[fail_id] failed with error cosensor[fail_error]
    goto sendAlert;
}

That way, if anything fails, I can skip all other checks and go straight through to the alert. Whoever is on call receives the alert and pulls up the full result of whatever function called the alert so that they can fix it.




回答4:


goto operator can be used to break multi-level loops.

This particular goto application example is given at goto manual page:

You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.




回答5:


Goto has been in large replaced by specialized statements like continue, break, try...catch. In the years I'm working with PHP, I didn't feel it could help me once. In languages like C++ which don't have break x statement it can be used to jump out of nested loops, but since PHP has it, there is no reason.

Code Complete has very nice and detailed chapter about gotos. Quoting the final two paragraphs:

Use of gotos is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten gotos with equivalent sequential constructs. In these simple cases, you should replace gotos out of habit. In the hard cases, you can still exorcise the goto in nine out of ten cases: You can break the code into smaller routines, use try-finally, use nested ifs, test and retest a status variable, or restructure a conditional. Eliminating the goto is harder in these cases, but it's good mental exercise and the techniques discussed in this section give you the tools to do it.

In the remaining one case out of 100 in which a goto is a legitimate solution to the problem, document it clearly and use it. If you have your rain boots on, it's not worth walking around the block to avoid a mud puddle. But keep your mind open to goto-less approaches suggested by other programmers. They might see something you don't.

I find it very hard to understand why the PHP team bothered to add it at all (after all those years we lived in peace without it).



来源:https://stackoverflow.com/questions/4843551/what-are-the-valid-use-cases-of-goto-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!