我创建了一个主类,所有我的主要成员激活失活的功能是,所有与之相关的其他事情也做拥有更高效的代码返回
这个主类(包含一些重要的功能一起)错误从不同的地方,包括通过卷曲
现在让我activaton功能(其中一个主要的功能)在
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
}
}
现在的问题是每个子函数调用后有if($this->_error){ return $this->_error;}
类(共我打电话一个m有大约35条这样的相似线) 我需要这个,因为我需要将错误发送给用户并阻止我的代码进一步运行。但它使我的代码长而且效率不高。 如何减少所有这些返回值,但在其中一个子函数失败时向用户显示错误,并尝试保留我的代码结构。 我必须调用每个主函数的各种子函数(这我不能改变,只有一个类和其中的各种函数),并且错误将主要在每个级别被捕获并返回(很少有简单的错误是没有返回并且代码被允许继续运行)。 我还必须记住,稍后可能会添加其他各种功能,并且它应该足够灵活,以便稍后处理所有内容。
我不认为代码执行效率真的是这里的问题。代码可维护性/可读性。 – 2013-05-03 21:01:02
是代码的可维护性/可读性肯定是一个问题,如果不是主要问题 – user1913849 2013-05-03 21:08:01