带命名空间的类方法中使用usort

31 3 月

usort在调用类方法时,第二个参数需要使用array,array第一项为类名,第二项为方法名。需要注意的是,该方法必须为静态方法,如果类是带命名空间的,需要写包含命名空间的类名称,节选代码如下:

namespace Home\Controller;

class TreeListController{

    static function sortFun($a, $b) {
        if ($a['index'] == $b['index']) {
            return 0;
        }
        return ($a['index'] > $b['index']?1:-1);
    }

    private function getList($type, $list = array()) {
        $navigations = C('NAVIGATION_LIST');
        $configs = $navigations[$type];
        usort($configs, array('Home\Controller\TreeListController', 'sortFun'));
        return $configs;
    }

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注