为什么这个array_filter方法没有调用这个函数?

Kor*_*gay 4 php callback array-filter

private static function returnSameElementIfNotEmpty($item) {
    if (empty($item)) {
        return false;
    }
    else{
        return true;
    }
}


public static function clean($array) {
    return array_filter($array, 'returnSameElementIfNotEmpty');
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用示例数组运行时,我得到:

警告:array_filter()要求参数2是有效的回调函数,函数'returnSameElementIfNotEmpty'未找到,或者在第27行的C:\ Framework\ArrayMethods.php中找到无效的函数名称

Nie*_*els 6

试试这个:

return array_filter($array, array(__CLASS__, 'returnSameElementIfNotEmpty'));
Run Code Online (Sandbox Code Playgroud)

发生错误是因为您没有调用类方法.但只是一个具有该名称的函数.在上面的例子中,我使用CLASS作为类类型来访问静态函数returnSameElementIfNotEmpty.