PHP usort() 期望参数 2 是一个有效的回调,而不是在一个类中 [英] PHP usort() expects parameter 2 to be a valid callback, not in a class

查看:32
本文介绍了PHP usort() 期望参数 2 是一个有效的回调,而不是在一个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 usort 不喜欢第二个参数(排序函数)有疑问.我在课堂上看到很多关于这是一个问题的问题,答案是传递一个 array($this, functionName)但就我而言,这是行不通的.该脚本不是一个类,但我收到此错误:

I have a problem with usort not liking the second parameter (the sorting function). I have seen a lot of questions about this being a problem when in a class, and the answer for that is to pass an array of array($this, functionName) But in my case this is not working. The script is not a class, but I am getting this error:

PHP Warning:  usort() expects parameter 2 to be a valid callback, function 'cmp' not found or invalid function name in <filename.php>

那么第二个参数,函数名,找不到是什么问题?这是直接来自 PHP 网站的示例代码.

So what is the problem with the second parameter, the function name, not being found? This is example code straight from the PHP website.

推荐答案

如果代码不在类中,但您使用的是命名空间,则 usort 期望第二个参数定义该命名空间.但不是在类似于在类中使用 usort 风格的数组中.

If the code is not in a class, but you are using a namespace, usort expects the second parameter to have that namespace defined. But not in an array in similar style to using usort in a class.

这对我有用,其中cmp"是排序功能:

This worked for me, where 'cmp' is the sorting function:

usort($arrayToSort, 'My\Full\Namespace\cmp');

这篇关于PHP usort() 期望参数 2 是一个有效的回调,而不是在一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆