PHP:'或'发表声明指令失败:如何把一个新的例外?
-
06-07-2019 - |
题
这里的每一个人都应该知道的'或'statemens,通常粘到一个die()命令:
$foo = bar() or die('Error: bar function return false.');
大部分的时候,我们看到的东西,如:
mysql_query('SELECT ...') or die('Error in during the query');
然而,我无法了解究竟如何,'或'的声明的工作。
我要扔一个新的例外,而不是死亡(),但是:
try{
$foo = bar() or throw new Exception('We have a problem here');
不工作,也不
$foo = bar() or function(){ throw new Exception('We have a problem here'); }
只有这样,我发现要做的就是这个可怕的想法:
function ThrowMe($mess, $code){
throw new Exception($mess, $code);
}
try{
$foo = bar() or ThrowMe('We have a problem in here', 666);
}catch(Exception $e){
echo $e->getMessage();
}
但是有一个办法扔一个新的例外直接后的"或"声明?
或这种结构是强制性的(我不像的ThrowMe功能在所有):
try{
$foo = bar();
if(!$foo){
throw new Exception('We have a problem in here');
}
}catch(Exception $e){
echo $e->getMessage();
}
编辑:我想要什么真的是避免使用一,如果()检查每一个潜在的危险的操作我这样做,例如:
#The echo $e->getMessage(); is just an example, in real life this have no sense!
try{
$foo = bar();
if(!$foo){
throw new Exception('Problems with bar()');
}
$aa = bb($foo);
if(!$aa){
throw new Exception('Problems with bb()');
}
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#But i relly prefer to use something like:
try{
$foo = bar() or throw new Exception('Problems with bar()');
$aa = bb($foo) or throw new Exception('Problems with bb()');
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#Actually, the only way i figured out is:
try{
$foo = bar() or throw new ThrowMe('Problems with bar()', 1);
$aa = bb($foo) or throw new ThrowMe('Problems with bb()', 2);
//...and so on!
}catch(Exception $e){
echo $e->getMessage();
}
#But i'll love to thro the exception directly instead of trick it with ThrowMe function.
解决方案
or
只是一个 逻辑运算器, ,这是类似于 ||
.
常见的伎俩
mysql_query() or die();
可能只是写的
mysql_query() || die();
这里发生了什么是"逻辑或"经营者(无论你选择)是想确定如果其中一个操作数的计算结果为正确的。该装置的操作数必须表达,可转换为一个布尔。
因此,原因
bar() or throw new Exception();
是非法的,是因为
(boolean)throw new Exception();
也是非法的。实质上,该进程的投掷异常不会产生回报的价值为操作者的检查。
但调用的一个函数 做 产生回报的价值对于操作者检查(没有明确回报的价值将会导致int的功能返回 NULL
这令人作 FALSE
)这就是为什么它的工作对于你的时候你总结的例外扔在一个功能。
希望这有所帮助。
其他提示
为什么不 bar()
和 bb()
抛出异常?在PHP异常中冒泡,所以不需要在调用 bar()
/ bb()
的函数/方法中抛出异常。 bar()
/ bb()
可能抛出这些异常。如果你想抛出另一个异常,你可以简单地做:
function foo() {
try {
$bar = bar();
} catch (BarException) {
throw new FooException;
}
}
我发现可以替换“或死亡()”的解决方案无处不在是用call_user_func立即调用的匿名函数包装throw:
call_user_func(function(){
throw new Exception("ERROR");
});
你可以通过在虚拟脚本上执行它来看到它的工作原理:
false or call_user_func(function(){throw new Exception("ERROR");});
我认为你想使用类似于最后一个结构的东西,尽管使用异常并没有意义:
$foo = bar();
if(!$foo){
echo 'We have a problem in here';
}
每条评论 - 我认为你不能在一行中做到这一点(即没有 if(!$ foo)
检查),我同意抛出异常的方法非常糟糕。就个人而言,我更喜欢明确的:
$foo = bar();
if(!$foo){
throw new Exception('We have a problem in here');
}
但这是个人偏好。如果你想要单行的东西,我认为你必须使用你的异常抛出函数选项。
我想这个限制可能取决于PHP的动态类型,它可以将函数调用的结果转换为条件,但不是 throw
的结果。
这是一个没有额外功能的单线解决方案:
if (!($foo = bar())) throw new Exception('We have a problem here');
我只是为此定义了函数 toss
。
function toss(Exception $exception): void
{
throw $exception;
}
因为当构造( new
)异常未被抛出( throw
)时捕获文件/行/堆栈信息,这不会t干扰调用栈。
所以你可以这样做。
something() or toss(new Exception('something failed'));
您还可以创建自定义异常类并使用它的静态构造方法而不是抛出新的Exception()
构造。
异常类:
class CustomException extends Exception {
static public function doThrow($message = "", $code = 0, Exception $previous = null) {
throw new Exception($message, $code, $previous);
}
}
用法:
try {
$foo = bar() or CustomException::doThrow('Problems with bar()');
$aa = bb($foo) or CustomException::doThrow('Problems with bb()');
} catch(Exception $e){
echo $e->getMessage();
}
注意强>
如果您使用的是PHP 7及更高版本 - 您可以将静态方法
doThrow()
重命名为throw()
,因为在PHP 7及更高版本中它可以使用保留关键字作为方法名称。