-3

所以我偶然发现了一些我没有意识到的事情:call_user_func_array显然中断了代码,但没有回过头来完成它的其余部分!换句话说,它就像 toreturnbreak调用exit它的当前函数一样工作,忽略后面的所有代码。

class B {
    function bar($arg1, $arg2) {
        $result = "$arg1 and $arg2";
        echo "Indeed we see '$result'.<br>\n";
        return $result;
    }
}

class A {
    function foo() {
        $args = ['apples', 'oranges'];
        echo "This line executes.<br>\n"
        $result = call_user_func_array(['B', 'bar'], $args);
        echo "This line never executes.<br>\n";
        echo "Thus we won't be able to use the phrase '$result' within this function.<br>\n";
    }
}

我们如何才能返回并完成剩下的部分foo

4

1 回答 1

1

我必须对您的代码进行一些更改才能使其正常工作。

class B {
    static function bar($arg1, $arg2) {
        $result = "$arg1 and $arg2";
        echo "Indeed we see '$result'.<br>\n";
        return $result;
    }
}

class A {
    static function foo() {
        $args = ['apples', 'oranges'];
        echo "This line executes.<br>\n";
        $result = call_user_func_array(['B', 'bar'], $args);
        echo "This line never executes.<br>\n";
        echo "Thus we won't be able to use the phrase '$result' within this function.<br>\n";
    }
}

A::foo();

但在那之后它又回来了:

This line executes.<br>
Indeed we see 'apples and oranges'.<br>
This line never executes.<br>
Thus we won't be able to use the phrase 'apples and oranges' within this function.<br>

请参阅:PHP 小提琴

请提供一个按照您所说的做的例子。

于 2021-11-21T10:27:25.947 回答