Possible to make "return false" "travel upward" in a chain of functions?

I have a function called within another function that if it returns false, I want the function that called it to return false. This is simple to do with if statements, [if ($function === false) { return false }] but it requires many if statements and complicates the code. (The nested functions become several layers deep at times.) Is it possible to make the entire function chain return false if one of the nested functions returns false without resorting to if statement in every function checking the return of the function that was called?

Are you wanting to halt further execution of the functions because of an error condition? It sounds like throwing an exception might be what you’re after.

If you wrap your initial function call in a try / catch block, then even if an exception is thrown in a function nested several levels deep, it will bubble up and be caught, allowing you to break out of the function’s execution.

2 Likes

It’s a different scenario, no real errors involved. The case is that a utility function is called to check for certain authorizations a user might have, and if the user does not have them, the function should return false (example: a session variable). But if it returns false, the rest of the code that called the utility function will continue to run, when it should also return false. I have called an exit so far, but this prevents my top-most layer of code from doing certain things, like monitoring for errors and performance. So I need certain nested functions to return false up the chain so that my top-most layer of code can still run.

I agree with @fretburner that you should use an exception.

function main() {
    try {
        helper1($something, $somethingelse);
    } catch (Exception $e) {
        // do something with the exception?
    }
    // more stuff, like logging etc
}

function helper1() {
    // do some stuff
    helper2();
    // some more stuff
}

function helper2() {
    // do some stuff
    helper3();
    // some more stuff
}

function helper3() {
    if (!has_access($something)) {
        throw new Exception('Access Denied', 403);
    }
    // more stuff here
}

so if the user doesn’t have access an exception will be thrown, which will be caught in main, and main can resume from there.

2 Likes

I am having trouble finding a way to use an exception here, because IF the user is not authorized to view an element of the page, they will still be able to view other parts of the page rendered later in the code. It’s actually expected user behavior, like visiting a user’s profile on a forum that requires you to be logged in to view it. You receive a prompt asking you to login, and then the footer and the rest of the page go on to render.

I guess my question is it is possible to bubble up a “return false”? From general scope issues, it doesn’t seem to be the case.

That’s where ScallioXTX’s try-catch can be useful.

try {
    // handle request
} catch (\YourProj\AccessDeniedException $e) {
    // generate error page, complete with header, footer, etc
}
1 Like

I didn’t consider making the catch block render something. Thanks to all for the valuable insight. I am little hesitant in using exceptions to control flow, but maybe I can make it work. I guess I thought exceptions were best left to catching a programmer’s error, but I can see the utility. I’ll try to consider how to make this work.

Actially, that’s more what error handling is for. Exception handling is for, well, handling exceptions :wink:

Exception handling is error handling. Exceptions were originally meant to replace traditional error reporting. Over time, programmers also started using them for control flow, which is why the mechanism gained the more generic name “exceptions”, for handling any kind of exceptional circumstance. Errors are considered to be one of those exceptional circumstances.

Some additional info.

The exception handling mechanism is provided to report and handle errors and exceptional events. However, the programmer must decide what it means to be exceptional in a given program. Can an event that happens most times a program is run be considered exceptional? Can an event that is planned for and handled be considered an error? The answer to both questions is yes. “Exceptional” does not mean “almost never happens” or “disastrous.” It is better to think of an exception as meaning “some part of the system couldn’t do what it was asked to do.”
— Stroustrup

2 Likes