ねえ、 ここにあなたのための質問です。PHPクラスのエラー処理
私は、PHPでクラスのエラー処理を選択するのに多くの時間を費やしました。 AjaxをPHPの例では
は、私はこのようにそれを行うクラスの取り扱い:エラー処理のためのよりよい解決策はあり :
public function setError($msg) {
$this->errors[] = $msg;
}
public function isFailed() {
return (count($errors) > 0 ? true : false); // if errors > 0 the request is failed
}
public function getJsonResp() {
if($this->isFailed()) {
$resp = array('result' => false, 'message' => $this->errors[0]);
} else {
$resp = array('result' => true);
array_merge($resp, $this->success_data); // the success data is set later
}
return json_encode($resp);
}
// an example function for a execution of a method would be this
public function switchMethod($method) {
switch($method) {
case 'create':
if(!isset($param1, $param2)) {
$this->setError('param1 or param2 not found');
} else {
$this->createSomething();
}
break;
default:
$this->setError('Method not found');
}
}
をだから私はのためにAKSに何をしたいあなたを知ることができますか?
すべての機能でtry&catchを使用する必要がありますか? – 000
いいえ、どこでエラーが発生する可能性がありますか、ほとんどの関数では、ゼロ除算例外のようなエラーを防ぐための単純なif文に頼ることができます。 – RobertPitt
お返事ありがとうございます – 000