I have a function that will perform a SELECT query from some db and return either:
我有一个函数将从某个数据库执行SELECT查询并返回:
false
(in case of error)假(如果有错误)
array(0) { }
一个空数组(0){}
关联数组
In order to test the return value of this function, is it good practice to do :
为了测试这个函数的返回值,最好是这样做:
$someVar = $this->someFunction();
if ($someVar) {
// ok, this is an associative array of result
} else {
// $someVar = false OR $someVar is an empty array
}
Or do I have to do something like this instead :
或者我必须做这样的事情:
$someVar = $this->someFunction();
if (is_array($someVar) && count($someVar) > 0) {
// ok, this is an associative array of result
} else {
// $someVar = false OR $someVar is an empty array
}
The first test seems to do what I want, but maybe I'm missing something that might go wrong after.
第一次测试似乎做了我想要的,但也许我错过了之后可能出错的东西。
So, is it good practice test arrays like I did in my first example?
那么,像我在第一个例子中所做的那样,测试数组是否很好?
2
Neither.
Returning false
to indicate error is fine at a very low level, but there should a layer between your low-level queries and your application code which inspects the return value and throws an exception on false
. The top-level controller which invokes your code should be handling those exceptions (which aren't caught earlier) and displaying a user-friendly error page in production, or dumping debugging information in development, while logging the error.
返回false表示错误在非常低的级别上很好,但是在低级查询和应用程序代码之间应该有一层检查返回值并在false时抛出异常。调用代码的顶级控制器应该处理这些异常(之前没有捕获)并在生产中显示用户友好的错误页面,或在开发过程中转储调试信息,同时记录错误。
There is absolutely no way you should be doing a three-way if/elseif/else
branch in your application to inspect the return values of every single database query. This is an incredibly dated way of checking for errors.
绝对没有办法在应用程序中执行三向if / elseif / else分支来检查每个数据库查询的返回值。这是一种检查错误的难以置信的过时方法。
Throw exceptions, and you can use your first form (if ($someVar)
) or, better yet:
抛出异常,你可以使用你的第一个表单(if($ someVar)),或者更好的是:
foreach ($this->someFunction() as $key => $row) {
}
1
http://php.net/manual/en/types.comparisons.php
An empty array evaluates to false. An array with values evaluates to true (even if all values would individually evaluate to false).
空数组的计算结果为false。值的数组计算结果为true(即使所有值都单独计算为false)。
You can also check if (empty($array))
but it is redundant to do so unless you are concerned about the array variable not being set.
您还可以检查是否为空($ array),但这样做是多余的,除非您担心未设置数组变量。
Finally, if the array is empty, $array == false
is true, but $array === false
is not.
最后,如果数组为空,$ array == false为true,但$ array === false不是。
1
According to the documentation on type comparisons, your first method is completely acceptable. An empty array will always evaluate to FALSE.
根据类型比较的文档,您的第一种方法是完全可以接受的。空数组总是计算为FALSE。
Documentation: http://php.net/manual/en/types.comparisons.php
That said, you may choose to handle no results and errors differently, e.g. logging the error message to a server log. In this case, you may want multiple if()
conditions.
也就是说,您可以选择不同地处理任何结果和错误,例如将错误消息记录到服务器日志。在这种情况下,您可能需要多个if()条件。
0
Neither. Test for all three cases:
都不是。测试所有三种情况:
if (false === $result) {
die('There is an error!');
} elseif (empty($result)) {
die('No results found');
}
foreach ($result as $foo) { ... }
If you really want to test for just two, then remember that foreach
will work in an empty array. So, this will work:
如果你真的只想测试两个,那么请记住foreach将在一个空数组中工作。所以,这将工作:
if (false === $result) {
die('There was an error!');
}
foreach ($result as $foo) { ... }
Or:
if (false === $result) {
echo 'There was an error!';
} else {
foreach ($result as $foo) { ... }
}
These last examples will simply give an empty page when the $result is an empty array. The first example will say that there are no results.
当$ result为空数组时,这些最后的示例将简单地给出一个空页面。第一个例子说没有结果。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/07/07/1496273ab665ab83b23e4a58d9cbb728.html。