库函数 exec_SELECTcountRows 的返回类型
Typo3 DB LIB 有一个名为 exec_SELECTcountRows
的方法。实现如下所示:
/**
* Counts the number of rows in a table.
*
* @param string $field: Name of the field to use in the
COUNT() expression (e.g. '*')
* @param string $table: Name of the table to count rows for
* @param string $where: (optional) WHERE statement of the query
* @return mixed Number of rows counter (integer) or
false if something went wrong (boolean)
*/
public function exec_SELECTcountRows($field, $table, $where = '') {
$count = FALSE;
$resultSet = $this->exec_SELECTquery('COUNT(' . $field . ')', $table, $where);
if ($resultSet !== FALSE) {
list($count) = $this->sql_fetch_row($resultSet);
$this->sql_free_result($resultSet);
}
return $count;
}
问题:
exec_SELECTcountRows
的返回类型确实是整数还是包含整数的字符串?
The Typo3 DB LIB has a method called exec_SELECTcountRows
. The implementation looks like this:
/**
* Counts the number of rows in a table.
*
* @param string $field: Name of the field to use in the
COUNT() expression (e.g. '*')
* @param string $table: Name of the table to count rows for
* @param string $where: (optional) WHERE statement of the query
* @return mixed Number of rows counter (integer) or
false if something went wrong (boolean)
*/
public function exec_SELECTcountRows($field, $table, $where = '') {
$count = FALSE;
$resultSet = $this->exec_SELECTquery('COUNT(' . $field . ')', $table, $where);
if ($resultSet !== FALSE) {
list($count) = $this->sql_fetch_row($resultSet);
$this->sql_free_result($resultSet);
}
return $count;
}
Question:
Is the return type of exec_SELECTcountRows
truly an integer or rather a string containing one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实你是对的。 PHPdoc 与行为不同(因为 mysql 函数以字符串形式返回值)。
我提交了错误报告,因为我认为应该更改为返回一个真正的整数。
Actually you're right. The PHPdoc differs from the behavior (as mysql functions return the value as string).
I filed a bug report, as I think this should be changed to return a true integer.
PHP 是松散类型的,字符串是整数,反之亦然。这取决于上下文。我确实认为该函数的返回类型将根据查询和上下文而变化。
例如,该函数可以返回布尔值
FALSE
。只要有实际的数据库结果要返回,基于
count(*)
的列通常会作为字符串返回,而不是整数(使用 MySQL 函数)。因此在这种情况下,返回类型实际上是字符串,而不是整数。但由于 PHP 是松散类型的,这并没有多大区别。注意间隙:
该函数可以返回字符串或
FALSE
。如果您编写单元测试,您还应该检查类型并在此处进行区分。该函数具有已记录的混合返回类型,因此首先断言它是否返回 FALSE。
然后,如果不返回 false,则如果需要从字符串中获取数字整数值,则断言返回值的 (int) 值。
PHP is loosely-typed, a string is an integer and vice-versa. It depends on context. I think truly that function's return-type will be changing depending on query and context.
For example the function can return
FALSE
which is a boolean.As long as there is an actual database result to return,
count(*)
based columns are normally returned as string, not integer (with MySQL Functions). So in that case the return-type is actually string, not integer. But as PHP is loosely typed, this does not make much of a difference.Mind the gap:
That function can either return a string or
FALSE
. If you write a unit test, you should check the type as well and differ here.The function has the mixed return type already documented, so assert first if it's returning FALSE or not.
Then if not returning false, assert the (int) value of the return value if you need to get the numerical integer value from the string.