在 PHP 的循环中继续冒泡
Continue bubbling up inside a loop in PHP
我改编了我编写的网络脚本以满足我对需要从服务器检索的一些数据的需求。我 运行 这个脚本来自终端,所以错误消息是有用的信息。
代码的主要部分是一个循环中的循环,在那个循环中我调用了一个函数。在这个函数中有一个对数据库的调用。如果连接到数据库时出现问题,我可以使用简单的 try {} catch(){}
捕获该错误,但是我应该如何构建我的代码以便我可以 跳过 此迭代并移动到循环中的下一个项目?换句话说,只在函数内执行 continue
。
以下是我的做法,但我不确定这是正确的方法。
foreach ($flavours as $icecream) {
foreach ($sauces as $sauce) {
$amount = dessertServings($icecream, $sauce);
if ($amount != null) {
// Some other functions like orderDessert, makePricingList and so on
fwrite(STDOUT, "$amount servings of $icecream with $sauce remaining!\n");
}
}
}
dessertServings($icecream, $sauce) {
try {
$dbConnection = new Connection("user", "password", "db$icecream$sauce");
$amountOfServings = $dbConnection->query($icecream, $sauce);
return $amountOfServings;
}
// E.g database connection could not be made
catch(Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
return;
}
}
有更好的方法吗?
为了让事情变得更难,如果函数实际上 return 没有任何东西 并且因此没有为变量赋值怎么办?你应该如何处理?
foreach ($flavours as $icecream) {
foreach ($sauces as $sauce) {
prepareDessert($icecream, $sauce);
// Other functions, most importantly: eatDessert($dessert)
}
}
prepareDessert($icecream, $sauce) {
try {
$dbConnection = new Connection("user", "password", "db$icecream$sauce");
$dbConnection->query($icecream, $sauce)->do("prepare");
}
// E.g database connection could not be made
catch(Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
}
}
在这种情况下,如何确保当try
失败时,循环中的块永远不会到达其他函数,我们不能吃一个一开始就没有准备好的冰淇淋!
我是否会使用一个空变量,简单地 return 在成功时为真,在失败时为假,并且仅在为真时在主块中执行以下代码?或者在 PHP?
中对此有更好的约定吗?
how should I structure my code so that I can just skip this iteration and move to the next item in the loop
异常处理的第一条规则:不做异常处理。允许它冒泡并在需要时捕获它(在你的情况下,在你的循环中)。
如果你想在函数内做一些处理(比如打印到 STDERR),你也可以在你的 catch 中重新抛出异常,但让它冒泡!
另一种更传统的方法是让函数 return 出现某种错误代码 - 最基本的是 "true" 成功,"false" 或 "null"像你的第一个例子一样失败。我看不出有什么不妥。
To make things harder, what if the function doesn't actually return anything and thus isn't assigning a value to a variable? How should you deal with that?
抛出异常,这是他们的工作!
how do I make sure that when the try fails, the block in the loop never reaches the other functions
不要在函数内 try/catch 或在 "catch".
内重新抛出异常
Would I use an empty variable that simply returns true on success and false and fail, and execute the following code in the main block only on true? Or is there a better convention for this in PHP?
是的,有一个更好的约定:抛出异常。
我改编了我编写的网络脚本以满足我对需要从服务器检索的一些数据的需求。我 运行 这个脚本来自终端,所以错误消息是有用的信息。
代码的主要部分是一个循环中的循环,在那个循环中我调用了一个函数。在这个函数中有一个对数据库的调用。如果连接到数据库时出现问题,我可以使用简单的 try {} catch(){}
捕获该错误,但是我应该如何构建我的代码以便我可以 跳过 此迭代并移动到循环中的下一个项目?换句话说,只在函数内执行 continue
。
以下是我的做法,但我不确定这是正确的方法。
foreach ($flavours as $icecream) {
foreach ($sauces as $sauce) {
$amount = dessertServings($icecream, $sauce);
if ($amount != null) {
// Some other functions like orderDessert, makePricingList and so on
fwrite(STDOUT, "$amount servings of $icecream with $sauce remaining!\n");
}
}
}
dessertServings($icecream, $sauce) {
try {
$dbConnection = new Connection("user", "password", "db$icecream$sauce");
$amountOfServings = $dbConnection->query($icecream, $sauce);
return $amountOfServings;
}
// E.g database connection could not be made
catch(Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
return;
}
}
有更好的方法吗?
为了让事情变得更难,如果函数实际上 return 没有任何东西 并且因此没有为变量赋值怎么办?你应该如何处理?
foreach ($flavours as $icecream) {
foreach ($sauces as $sauce) {
prepareDessert($icecream, $sauce);
// Other functions, most importantly: eatDessert($dessert)
}
}
prepareDessert($icecream, $sauce) {
try {
$dbConnection = new Connection("user", "password", "db$icecream$sauce");
$dbConnection->query($icecream, $sauce)->do("prepare");
}
// E.g database connection could not be made
catch(Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
}
}
在这种情况下,如何确保当try
失败时,循环中的块永远不会到达其他函数,我们不能吃一个一开始就没有准备好的冰淇淋!
我是否会使用一个空变量,简单地 return 在成功时为真,在失败时为假,并且仅在为真时在主块中执行以下代码?或者在 PHP?
中对此有更好的约定吗?how should I structure my code so that I can just skip this iteration and move to the next item in the loop
异常处理的第一条规则:不做异常处理。允许它冒泡并在需要时捕获它(在你的情况下,在你的循环中)。
如果你想在函数内做一些处理(比如打印到 STDERR),你也可以在你的 catch 中重新抛出异常,但让它冒泡!
另一种更传统的方法是让函数 return 出现某种错误代码 - 最基本的是 "true" 成功,"false" 或 "null"像你的第一个例子一样失败。我看不出有什么不妥。
To make things harder, what if the function doesn't actually return anything and thus isn't assigning a value to a variable? How should you deal with that?
抛出异常,这是他们的工作!
how do I make sure that when the try fails, the block in the loop never reaches the other functions
不要在函数内 try/catch 或在 "catch".
内重新抛出异常Would I use an empty variable that simply returns true on success and false and fail, and execute the following code in the main block only on true? Or is there a better convention for this in PHP?
是的,有一个更好的约定:抛出异常。