webman 调用函数,被调用函数怎么不返回,直接返回给用户呢

webmanchin

webman 调用函数,被调用函数怎么不返回,直接返回给用户呢
比如用fpm是直接exit 停止并输出

webman 该怎么实现这个

826 3 0
3个回答

six

return

  • webmanchin 2022-09-09

    被调用的函数 return 只能返回给调用函数,不能直接输出的用户界面

2548a

现阶段没有办法,只有抛异常或者return

  • webmanchin 2022-09-09

    被调用的函数 return 只能返回给调用函数,不能直接输出的用户界面

MarkGo

通过throw 不同异常,再调用函数里处理,就可以避开。
简单的:

function a(int $num){
    try{
        $res = test($num);
        $res = test($num+1);
        $res = test($num+2);
        return \response($res);
    }catch(\Throwable $e){
        return \response($e->getMessage());
    }
}

function test($num){
    if($num%2){
        throw new Exception('message',1112233);
    }
    return $num;
}
  • webmanchin 2022-09-09

    谢谢,非常给力

  • six 2022-09-09

    也可以封装成一个函数

    function quit($msg) {
        throw new \Exception($msg);
    }

    就可以像exit die 那样愉快的调用了

年代过于久远,无法发表回答
🔝