开发者

Return Type of Library function exec_SELECTcountRows

开发者 https://www.devze.com 2023-03-30 09:48 出处:网络
The Typo3 DB LIB has a method called exec_SELECTcountRows. The implementation looks like this: /** * Counts the number of rows in a table.

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 开发者_JAVA百科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?


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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消