Hey there, here is a question for you guys.
I have so much times to choose a error handling for classes in PHP.
For Example in Ajax PHP Handling Classes i do it this way:
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');
}
}
So lets know you what i want to aks for: Is there a better solution for error handling?