首页 > 解决方案 > 解决 php 中的嵌套条件(错误)级别

问题描述

我正在尝试解决 php 中与错误处理相关的嵌套条件级别。

我已经在这里阅读了一些问题和答案,例如扁平化的东西。

但是......想象一下像这样的功能

function my_function() {

    // 1. Doing things here
    // ...


    // 2. Handle something with text here
    $foo = 'some text ...';

    $foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error

    $foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error

    // Final functions doing things with the text


    // 3. Doing other things here

    return TRUE;

}

为了做正确的事情,我应该做错误处理:

function my_function() {

    // 1. Doing things here
    // ...


    // 2. Handle something with text here
    $foo = 'some text ...';

    $foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
    if (FALSE === $foo) {
        // Handle error, e.g. skip to next flow after code block
    }

    $foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
    if (FALSE === $foo) {
        // Handle error, e.g. skip to next flow after code block
    }

    // Final functions doing things with the text


    // 3. Doing other things here

    return TRUE;

}   

也许你已经看到了我的问题:处理错误需要我嵌套ifs

    $foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
    if (FALSE === $foo) {
        // Handle error, e.g. skip to next flow after code block
    }
    else {
        $foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
        if (FALSE === $foo) {
            // Handle error, e.g. skip to next flow after code block
        }
        else {
            // Final functions doing things with the text
        }
    }

执行最终函数,因为从函数返回不是一个选项(步骤 3 应该独立于 2.happend 中的内容执行)。

您知道如何“展平”这些嵌套条件吗?

标签: phpnestedconditional-statements

解决方案


您可以稍微更改代码设计,然后从您的函数中提前返回:

function my_function() {  
    $foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
    if (FALSE === $foo) {
        // Handle error, e.g. skip to next flow after code block
        return FALSE;
    }

    $foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
    if (FALSE === $foo) {
        // Handle error, e.g. skip to next flow after code block
        return FALSE;
    }

    // Final functions doing things with the text
    return TRUE;
}

function outer_function() {
    $result = my_function()

    // 3. Doing other things here

    // Do things with $result
}

另一种处理方法是使用try--catchfinally

function my_function() {  
    try {
        $foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
        if (FALSE === $foo) { throw new Exception('function_a failed'); }

        $foo = function_b($foo); // Also handling the text, doing something, 
        if (FALSE === $foo) { throw new Exception('function_b failed'); }
    } catch (Exception $e) {
        // Handle error, e.g. skip to next flow after code block
        // echo error messages
        return FALSE;
    } finally {
        // Final functions doing things with the text whether successful or not
    }
    // 3. Doing other things here if all successful
    return TRUE;
}

我更喜欢第二种方式,因为它感觉更干净,如果有错误,finally语句将在返回之前执行。如果您的内部函数自己引发异常并合并所有错误处理,FALSE您可以避免使用这些语句。throw


推荐阅读