为什么我在使用 PHP 时会收到“仅应通过引用传递变量”错误?

Rob*_*ion -1 php arrays

我收到此错误:-

严格的标准:只有变量应该通过引用传递

……我不知道发生了什么;这段代码中发生了错误:

public function prepare_items() {
    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();
    $this->_column_headers = array($columns, $hidden, $sortable);
    $this->items = $this->flagged_message();

    usort( $this->flagged_message(), array( &$this, 'usort_reorder' ) );
    $this->items = $this->flagged_message();        

}
Run Code Online (Sandbox Code Playgroud)

这行是错误:

 usort( $this->flagged_message(), array( &$this, 'usort_reorder' ) );
Run Code Online (Sandbox Code Playgroud)

flagged_message()功能是:

public function flagged_message(){
    global $wpdb;
    $improper_contents = $wpdb->get_results( $wpdb->prepare("SELECT comment_id FROM pw_commentmeta WHERE meta_key = 'flag_this_message' AND meta_value = '%d'", $this->flagged_id) );
    $flagged = [];
    if($improper_contents){
        foreach ($improper_contents as $improper_content_arr) {

        $comment_id = (int)$improper_content_arr->comment_id;
        $comment = get_comment( $comment_id );
        $comment_content = $comment->comment_content;
        $comment_author = $comment->comment_author;
        $comment_post_ID = $comment->comment_post_ID;
        $comment_date = $comment->comment_date;
        $comment_who = $comment->user_id;

        $flagged[] = array(
            "ID"                =>  $comment_id,
            "username"          =>  $comment_author,
            "flagged_message"   =>  $comment_content,
            "date"              =>  $comment_date,
        );

        }

    }
    return $flagged;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会出现错误,有人可以向我解释一下吗?

Han*_*nky 5

乌排序

bool usort ( array &$array , callable $value_compare_func )
Run Code Online (Sandbox Code Playgroud)

需要通过引用对数组进行排序,并且您将其function作为第一个参数发送。就是这样。

我知道您的函数返回一个数组,但您需要将其存储在变量中,以便将其引用传递给usort. 如果不先将函数的返回值保存在变量中,则函数的返回值在其作用域之外是未知的,无法在另一个函数中引用。如果usort没有想到通过引用传递该参数,那么您的代码就可以了。

任何按值调用函数都会接受这一点。

笔记

您很可能错误地设置了错误的参数顺序。因为你的第二个参数似乎是一个数组,这也是错误的。第一个应该是 a array,第二个应该是 a callable,你把它们颠倒了。但即便如此,这也不是真正的调用方式usort

你的电话应该是这样的

$messages=$this->flagged_message();
usort($messages , 'usort_reorder');
Run Code Online (Sandbox Code Playgroud)