从子函数中打破父函数(PHP优先) [英] Breaking a parent function from within a child function (PHP Preferrably)

查看:193
本文介绍了从子函数中打破父函数(PHP优先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了如何在不修改父代码的情况下破解或终止父函数的执行,使用PHP

I was challenged how to break or end execution of a parent function without modifying the code of the parent, using PHP

我找不到任何解决方案,除了死();在孩子中,这将终止所有的执行,所以在父函数调用之后的任何事情都会结束。任何想法?

I cannot figure out any solution, other than die(); in the child, which would end all execution, and so anything after the parent function call would end. Any ideas?

代码示例:

code example:

function victim() {
    echo "I should be run";
    killer();
    echo "I should not";
}
function killer() {
    //code to break parent here
}
victim();
echo "This should still run";


推荐答案

function victim() {
    echo "I should be run";
    killer();
    echo "I should not";
}
function killer() {
    throw new Exception('Die!');
}

try {
    victim();
} catch (Exception $e) {
    // note that catch blocks shouldn't be empty :)
}
echo "This should still run";

这篇关于从子函数中打破父函数(PHP优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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