I have a cakephp application, in one of my controllers, I have a function _filter_callback to be used as callback to the php function array_filter() like this:
class TestClassController extends AppController
{
var $name = 'TestClass';
....
function test_fn()
{
...
array_filter($my_array, '_filter_callback');
}
function _filter_callback($val)
{
// callback logic here...
}
}
The problem I have is that my function _filter_callback cannot be found by array_filter in my controller because I get the following error:
Warning: array_filter() expects parameter 2 to be a valid callback,
function '_filter_callback' not found or invalid function name
Does anybody know how I can make array_filter aware of my callback function in a cakephp application?
Is the function in the global scope? If not – it has to be.
If the body of the function is simple (in most cases) try to use anonymous functions as of PHP > 5.3 – http://php.net/manual/en/functions.anonymous.php
or use the create_function
http://php.net/manual/en/function.create-function.php
You can then inject this as a parameter without declaring one-use global function.
Example of filtering all elements smaller than 10:
EDIT:
I can see now that you are trying to call method of the object. You must specify the instance. Try this: