我在我的申请中收到了一个奇怪的警告通知.我usort在一个类中使用自定义函数.这是它的样子:
class Class_Name
{
function zstream_builder()
{
$array = some_array();
//sort posts by date DESC
usort($array, array('Class_Name', 'zstream_sorter')); // <- the notice is thrown on this line
return $array;
}
private static function zstream_sorter($key = 'sort_str_date')
{
return function ($a, $b) use ($key)
{
return strnatcmp($a[$key], $b[$key]);
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的通知:
Notice: Object of class Closure could not be converted to int in PATH_TO_FILE on line xx
有任何想法吗?
usort将调用Class_Name::stream_sorter作为比较函数,传递两个参数.返回值是一个函数,但usort需要一个整数来告诉它哪个参数更大.你需要传递to的返回值,Class_Name::stream_sorter而usort不是函数本身:
usort($array, self::zstream_sorter());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |