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


public static function clean($array) {
    return array_filter($array, 'returnSameElementIfNotEmpty');
}

When I try running this with a sample array I get:

Warning: array_filter() expects parameter 2 to be a valid callback, function 'returnSameElementIfNotEmpty' not found or invalid function name in C:\Framework\ArrayMethods.php on line 27

2 Answers 2

6

Try this:

return array_filter($array, array(__CLASS__, 'returnSameElementIfNotEmpty'));

The error occures because you don't call the class method. But just a function with that name. In the above example I use CLASS as the class type to access the static function returnSameElementIfNotEmpty.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried like: return array_filter($array, CLASS.'::returnSameElementIfNotEmpty'); and it worked, but why is it so?
See my answer, I've added a small part.
I tried with self::returnSameElementIfNotEmpty() , that did not help either?
1

Excellent, in the documentation not mentioned.

array(CLASS, 'returnSameElementIfNotEmpty') solves warning

More elegant:

$ArrModEmpty = array_filter($array, function($Arr){
                return (empty($Arr));
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.