dream0614 2013-03-06 10:13
浏览 35
已采纳

在OOP中的php uasort

class DBNews {

    public function get_latest_posts($limit){

        // code goes here

        $posts_array = array();
        uasort($posts_array, $this->cmp);

    }

    public function cmp($a, $b) {
        if ($a == $b) {
            return 0;
        }

        return ($a < $b) ? -1 : 1;
    }
}

I get the following warning :

Warning: uasort() expects parameter 2 to be a valid callback, 
no array or string given in 
C:\xampp\htdocs
ews\admin\functions.php on line 554.

And the 554th line contains uasort($posts_array, $this->cmp).

Where to make use of the string or array and in what way?

EDIT : If I use uasort($posts_array, array($this, 'cmp'));, I get the following warning:

uasort() expects parameter 2 to be a valid callback,
array must have exactly two members in
C:\xampp\htdocs
ews\admin\functions.php on line 554
  • 写回答

3条回答 默认 最新

  • dongpo9071 2013-03-06 10:25
    关注

    If you have >= 5.3 and you're not using the compare method in any other function, you can also use closures:

    uasort($posts_array, function($a, $b) {
        if ($a == $b) {
            return 0;
        }
    
        return ($a < $b) ? -1 : 1;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?