zend_play Posted May 4, 2013 Share Posted May 4, 2013 I have created a main class where all my main member activation deactivation functions are and all the other things related to them are also donethis main class(along with some main functions) is called from various places including through curlNow lets take my activaton function(One of the main functions) in the class activationFunction($data) { //use data to generate total, discount etc $this->giveAffiliates($total); if($this->_error){ return $this->_error;} $this->activateOrder($total,$discount,id); if($this->_error){ return $this->_error;} $this->activatePlan($total,$discount,id); if($this->_error){ return $this->_error;} //similarily calling various functions which themselves call other functions } activatePlan() { try{ //call other functions and do necessary stuff for plan A } catch(Exception $e) { $this->_error.="Error occurred while activating plan A"; } //for plan B try{ //call other functions and do necessary stuff for plan B } catch(Exception $e) { $this->_error.="Error occurred while activating plan B"; } //for other plans similarily } } Now the issue is having if($this->_error){ return $this->_error;} after each sub function call.(Total I am having around 35 such similar lines)I require this as I need to send the error to the user and stop my code from running further.But it is making my code long and not efficient.How can I reduce all these returns but show the user an error when one of the sub functions fails and try to keep my code structure as is.I have to call the various subfunctions from each main function(This I cannot change, there will be only one class and various functions in it) and the errors will mostly have to be caught at each level and returned(very few simple errors are not returned and the code is allowed to continue running) .I have to also keep in mind that there can be various other functions be added to it later and it should be flexible enough to handle all that later Quote Link to comment https://forums.phpfreaks.com/topic/277615-having-more-efficientmaintanablereadable-code-to-return-errors/ Share on other sites More sharing options...
ignace Posted May 4, 2013 Share Posted May 4, 2013 (edited) It's a standard try/catch: try { $this->activateFunction(); $this->activatePlan(); .. } catch (ComponentActivationFailed $e) { return $e->getMessage(); } class ComponentActivationFailed extends Exception {}If you need to specifically know which component failed: try { $this->activateFunction(); $this->activatePlan(); .. } catch (FunctionActivationFailed $e) { .. } catch (PlanActivationFailed $e) { .. } catch (ComponentActivationFailed $e) { // catch-all } class FunctionActivationFailed extends ComponentActivationFailed {} class PlanActivationFailed extends ComponentActivationFailed {} Edited May 4, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/277615-having-more-efficientmaintanablereadable-code-to-return-errors/#findComment-1428175 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.