Why doesn't this array_filter method call this function?

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 to run this using the sample array, I get:

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

+4
source share
2 answers

Try the following:

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

The error occurs because you do not call the class method. But just a function with that name. In the above example, I use CLASS as a class type to access the static function returnSameElementIfNotEmpty .

+6
source

Great, the documentation is not mentioned.

array ( CLASS , 'returnSameElementIfNotEmpty') resolves the warning

More elegant:

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

All Articles