【发布时间】:2011-02-13 19:53:41
【问题描述】:
你好, 这里有一个问题要问你们。
我有很多时间为 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');
}
}
所以让你知道我想要什么: 有没有更好的错误处理解决方案?
【问题讨论】:
标签: php class error-handling