打破PHP中的多个函数(短路)
我想在 PHP 中返回多个嵌套函数。通过在“break”后添加数字可以跳出多个循环。例如。
while(1)
while(1)
while(1)
break 3;
我可以在调用一系列函数时进行断路吗?
I want to return multiple nested functions in PHP. It's possible to break out of multiple loops by adding a number after "break". Eg.
while(1)
while(1)
while(1)
break 3;
Can I do a circuit break while calling a sequence of functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
据我所知,这种设计也不是很健康,因为相关的父母和祖父母功能永远不会知道中断。您应该抛出一个异常并在父级上捕获它,而父级又将抛出一个异常并在祖级上捕获它,等等。
Not that I know of, it's also not very healthy of a design, as the parent and grandparent functions in question will never know of the break. You should throw an exception and catch it on the parent, which in turn will throw an exception and catch it on the grandparent etc.
要“中断”函数,您可以使用
return
。To "break" out of functions, you can use the
return
.另一种解决方案是为每个 while 添加一个条件。
尽管我不认为这是一个非常干净的方法。
Another solution would be to add a condition to each while.
Although I don't think this is a very clean approach.
正如手册所述 中断仅适用于循环。
As the manual states break is for loop only.
在这种情况下,我所做的是有一个异常返回值(或对象),并在每个函数返回点对返回值进行值检查,以确保这种情况得到适当的传播或处理,但在进行递归时要小心,你可能会错误地完全折叠树......顺便说一句,如果这是一个简单的错误退出类型的情况,您也可以使用异常。
What I do in such cases is that have an exception return value(or object) and do value check on return value at every function return point to make sure that the situation is propagated or handled appropriately, be careful while doing recursions though, you might completely fold up the tree by mistake....btw if it is a simple exit on error kind of situation you can also use exceptions.
子函数可以返回一个特殊结果,表明已满足特定条件。 WordPress 使用 WP_Error 和
is_wp_error()
来进行此类操作。任意数量的嵌套函数都可以检查被调用函数是否返回错误状态,并选择将该错误传递到链上而不是继续处理。例子:
It's possible to return a special result from child functions that indicates a specific condition has been met. WordPress uses WP_Error and
is_wp_error()
for this sort of operation. Any number of nested functions can check to see if a called function returned an error state, and opt to pass that error up the chain rather than continue with processing.Example:
是的,您可以非常简单地构造一个“无主体”
while()
或if()
块。通常,您会看到 PSR-12 兼容的 PHP 代码使用{}
来结束循环/条件块的主体,但主体不是必需的。在行尾编写一个分号就足够了,您的 IDE 不会抱怨语法错误。从每个函数返回一个真值将足以表明以下函数已被授权执行。
这将提供所需的“短路”功能,而无需创建嵌套控制结构或将变量传递到不同的作用域。
我将演示一组通用函数:
代码:(具有更多场景的演示)
Yes, you can very simply construct a "body-less"
while()
orif()
block. Typically, you will see PSR-12 compliant PHP code using{}
to bookend the body of the loop/condition block, but the body is not required. Writing a semicolon at the end of the line will be sufficient and your IDE will not complain about bad syntax.Returning a truthy value from each function will be an adequate indicator that the following function is authorised for execution.
This will provide the "short circuit" functionality that is desired without creating nested control structures or passing variables into different scopes.
I'll demonstrate with a battery of generic functions:
Code: (Demo with more scenarios)