php - 解决 php 中的嵌套条件(错误)级别
问题描述
我正在尝试解决 php 中与错误处理相关的嵌套条件级别。
我已经在这里阅读了一些问题和答案,例如扁平化的东西。
但是......想象一下像这样的功能
function my_function() {
// 1. Doing things here
// ...
// 2. Handle something with text here
$foo = 'some text ...';
$foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
$foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
// Final functions doing things with the text
// 3. Doing other things here
return TRUE;
}
为了做正确的事情,我应该做错误处理:
function my_function() {
// 1. Doing things here
// ...
// 2. Handle something with text here
$foo = 'some text ...';
$foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
}
$foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
}
// Final functions doing things with the text
// 3. Doing other things here
return TRUE;
}
也许你已经看到了我的问题:处理错误需要我嵌套if
s
$foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
}
else {
$foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
}
else {
// Final functions doing things with the text
}
}
执行最终函数,因为从函数返回不是一个选项(步骤 3 应该独立于 2.happend 中的内容执行)。
您知道如何“展平”这些嵌套条件吗?
解决方案
您可以稍微更改代码设计,然后从您的函数中提前返回:
function my_function() {
$foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
return FALSE;
}
$foo = function_b($foo); // Also handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) {
// Handle error, e.g. skip to next flow after code block
return FALSE;
}
// Final functions doing things with the text
return TRUE;
}
function outer_function() {
$result = my_function()
// 3. Doing other things here
// Do things with $result
}
另一种处理方法是使用try
--catch
finally
块。
function my_function() {
try {
$foo = function_a($foo); // Handling the text, doing something, returning FALSE in case of error
if (FALSE === $foo) { throw new Exception('function_a failed'); }
$foo = function_b($foo); // Also handling the text, doing something,
if (FALSE === $foo) { throw new Exception('function_b failed'); }
} catch (Exception $e) {
// Handle error, e.g. skip to next flow after code block
// echo error messages
return FALSE;
} finally {
// Final functions doing things with the text whether successful or not
}
// 3. Doing other things here if all successful
return TRUE;
}
我更喜欢第二种方式,因为它感觉更干净,如果有错误,finally
语句将在返回之前执行。如果您的内部函数自己引发异常并合并所有错误处理,FALSE
您可以避免使用这些语句。throw
推荐阅读
- reactjs - 从 Formik onSubmit 处理程序更新状态导致 useEffect 触发两次
- c# - 如何在 ASP.NET Core 中配置端点路由
- kotlin-coroutines - 在协程中将事件从一个流发送到另一个流
- angular - @CrossOrigin 不适用于 spring-boot-starter-web 2.5.2
- android - 为什么 layout_constraintStart_toEndOf 总是与 ConstraintLayout 中的第一个控件重叠?
- python - 每个项目(子列表)都是在 .sort() 函数上显示 None 的列表
- python - 二叉树一次向左 XOR 向右移动一个节点
- javascript - 如何解决方法的错误?axios.post
- java - POSTMAN/Java 中的文件传输。需要找到 curl -T 等价物
- c++ - 如何在 cgal Dd Delaunay Triangulations 中修改(移动)点