简体   繁体   English

在PHP上退出内部函数

[英]Exit inside function on PHP

Can I use this exit on a public api php OR it's an unsafe method? 我可以在公共api php上使用此出口吗,或者这是一种不安全的方法? I'm asking that because I want to show the error to api user without back to mainFunction. 我问这个问题是因为我想向api用户显示错误而不返回mainFunction。

class Apiclass{
    private extraPrivateFunction($var){
        if($var == 1){ 
          exit(json_encode(array('status' => 'error','message' => 'error 1 is 1'))); 
        }
    }
    public mainFunction(){
        $number = 1;
        $this->extraPrivateFunction($number); 
    }
}

Thx guys. 谢谢你们

Yes, it works, just try this code: 是的,它可以工作,只需尝试以下代码:

class Apiclass{
    private function extraPrivateFunction($var){
        if($var == 1){ 
          exit(json_encode(array('status' => 'error','message' => 'error 1 is 1'))); 
        }
    }

    public function mainFunction(){
        $number = 1;
        $this->extraPrivateFunction($number); 

        echo "Never Gets Here";
    }
}

$a = new Apiclass();
$a->mainFunction();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM