Jump to content

Recommended Posts

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 done

this main class(along with some main functions) is called from various places including through curl

Now 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

 

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 by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.