PHP 中 goto 的有效用例有哪些?

发布于 2024-10-14 21:16:41 字数 1431 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-10-21 21:16:41

一种可能的用途(但是可以使用其他方式实现)是实现有限状态机。

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

琉璃繁缕 2024-10-21 21:16:41

请参阅 PHP 架构师 - PHP 5.3 中的 GOTO:真的那么邪恶吗?

本文简要概述了为什么 GOTO 很有用、为什么我们在 PHP 中使用它(自 2004 年以来)以及为什么它曾经/现在是有争议的。一般的答案似乎是:GOTO 几乎没有用,应该避免,除非在非常狭窄的应用程序空间中(例如构建编译器):

有一件事是肯定的:在某些应用程序中,优秀的开发人员可以充分利用 GOTO 来生成更简单、更高效的代码,但它们可能不是普通开发人员编写的主流程序;因此,这里有一个合理的担忧,即缺乏经验的程序员会使用 GOTO 来损害他们的代码。

另请参阅文章中提到的一些原始新闻列表讨论/争议:

另请参阅有关 GOTO 在 C2 Wiki 中被认为是有害的

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:

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

浮云落日 2024-10-21 21:16:41

我使用它的一种情况是,当我需要进行多级检查并且不想添加额外的代码时,我可以在检查后跳转到相关部分。

例如,有这样一个系统,它检查仓库中各种机器的状态,如果任何一个系统的任何一个部分出现故障,整个仓库都会出现故障,并发出警报,在伪代码中它是这样的

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;
}

:这样,如果任何事情失败,我可以跳过所有其他检查并直接进入警报。无论谁值班,都会收到警报并提取调用警报的任何函数的完整结果,以便他们可以修复它。

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.

绾颜 2024-10-21 21:16:41

goto 运算符可用于中断多级循环。

这个特定的goto应用程序示例在goto手册中给出页面

您也不能跳入任何类型的循环或开关结构。您可以跳出这些,常见用途是使用 goto 代替多级中断。

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.

破晓 2024-10-21 21:16:41

Goto 已在很大程度上被专门的语句所取代,例如 continuebreaktry...catch。在我使用 PHP 的这些年里,我从来没有觉得它对我有帮助。在像 C++ 这样没有 break x 语句的语言中,它可以用来跳出嵌套循环,但既然 PHP 有它,那就没有理由了。

Code Complete 有关于 goto 的非常好的详细章节。引用最后两段:

goto 的使用是一个宗教问题。我的教条是,在现代语言中,您可以轻松地将十分之九的 goto 替换为等效的顺序结构。在这些简单的情况下,您应该出于习惯而替换 goto。在困难的情况下,您仍然可以在十分之九的情况下消除 goto:您可以将代码分解为更小的例程,使用 try-finally,使用嵌套的 if,测试并重新测试状态变量,或者重构条件。在这些情况下,消除 goto 会比较困难,但这是很好的心理锻炼,本节讨论的技术为您提供了实现这一目标的工具。

在剩下的 100 种情况中,goto 是问题的合法解决方案,请清楚地记录并使用它。如果你穿着雨鞋,那么为了避免陷入泥坑而在街区周围走动是不值得的。但请对其他程序员建议的无 goto 方法保持开放的态度。他们可能会看到您看不到的东西。

我发现很难理解为什么 PHP 团队要费心添加它(毕竟这么多年我们在没有它的情况下和平地生活了)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文