本文介绍了PHP类中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有很多时间为PHP中的类选择错误处理。 p> 例如在Ajax PHP处理类中,我这样做: public function setError($ msg){ $ this-> errors [] = $ msg; } public function isFailed(){ return(count($ errors)> 0?true:false); // if errors> 0请求失败} 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); //成功数据设置为} return json_encode($ resp); } //一个执行方法的示例函数是这个 public function switchMethod($ method){ switch($ ($ param1,$ param2)){ $ this-> setError('param1 or param2 not found'); } else { $ this-> createSomething(); } break; default: $ this-> setError('Method not found'); } } 所以让我知道你想要的东西: 是否有更好的解决方案来处理错误?解决方案谈到OOP最好的办法是使用例外来处理您的错误,例如: class示例extends BaseExample实现IExample { public function getExamples() { if($ this-> ExamplesReady === false) { throw new ExampleException(example are not ready。); } } } class ExampleException extends Exception {} 在你的类中抛出异常,并捕获抛出它们的类之外的异常是我通常去做的事情。 用法示例: $ Example = new Example(); try { $ Examples = $ Example-> getExamples(); foreach($ Examples as $ Example) { // ... } } catch(ExampleException $ e) { Registry :: get(Output) - > displayError(Unable to perform action,$ e); } 和您的 displayError 将使用 $ e-> getMessage()作为有关错误的信息。 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 thispublic 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? 解决方案 When it comes to OOP Your best bet is to use Exceptions to handle your errors, for example:class Example extends BaseExample implements IExample{ public function getExamples() { if($this->ExamplesReady === false) { throw new ExampleException("Examples are not ready."); } }}class ExampleException extends Exception{}throwing exceptions within your class and catching exceptions outside of the classes that throw them is the way I usually go about things.Usage Example:$Example = new Example();try{ $Examples = $Example->getExamples(); foreach($Examples as $Example) { //... }}catch(ExampleException $e){ Registry::get("Output")->displayError("Unable to perform action",$e);}and your displayError would use $e->getMessage() as the information regarding the error. 这篇关于PHP类中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 04:29