有什么办法可以从内部函数中退出外部函数? [英] Any way to exit outer function from within inner function?

查看:106
本文介绍了有什么办法可以从内部函数中退出外部函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,如果我有一个函数调用了另一个函数;有什么方法可以使被调用的函数退出调用方函数而不会杀死整个脚本?



例如,假设我有一些类似以下的代码:

 <?php 
函数funcA(){
funcB();
回声你好,我们完成了funcB;
}

函数funcB(){
echo‘This is funcB’;
}
?>
< p>这是一些文本。这段文字之后,我将称呼funcA。< / p>
< p><?php funcA(); && lt; / p>
< p>这是funcA运行后的更多文字。

不幸的是,如果我发现funcB内部有一些东西使我想阻止funcA完成,我似乎必须退出整个PHP脚本。



我知道我可以在funcA()中写一些东西来检查funcB()的结果,但是,就我而言,我无法控制funcA()的内容;我只能控制funcB()的内容。



使这个例子更加具体;在这种情况下,我正在使用WordPress。我正在使用 get_template_part( )函数,并尝试通过执行我的钩子后调用的locate_template()函数阻止WordPress实际需要/包含文件。



有人吗?有什么建议吗?

解决方案

抛出 funcB 中没有在 funcA <中处理的exceptions.php rel = nofollow>例外 / p>

Within PHP, if I have one function that calls another function; is there any way to get the called function to exit out of the caller function without killing the entire script?

For instance, let's say I have some code something like:

<?php
function funcA() {
    funcB();
    echo 'Hello, we finished funcB';
}

function funcB() {
    echo 'This is funcB';
}
?>
<p>This is some text. After this text, I'm going to call funcA.</p>
<p><?php funcA(); ?></p>
<p>This is more text after funcA ran.</p>

Unfortunately, if I find something inside of funcB that makes me want to stop funcA from finishing, I seem to have to exit the entire PHP script. Is there any way around this?

I understand that I could write something into funcA() to check for a result from funcB(), but, in my case, I have no control over the contents of funcA(); I only have control over the contents of funcB().

To make this example a little more concrete; in this particular instance, I am working with WordPress. I am hooking into the get_template_part() function, and trying to stop WordPress from actually requiring/including the file through the locate_template() function that's called after my hook is executed.

Does anyone have any advice?

解决方案

Throw an exception in funcB that is not handled in funcA

这篇关于有什么办法可以从内部函数中退出外部函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆