Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking a parent function from within a child function (PHP Preferrably)

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";
like image 675
Franklyn Tackitt Avatar asked Jul 01 '10 01:07

Franklyn Tackitt


1 Answers

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";
like image 188
deceze Avatar answered Sep 29 '22 22:09

deceze