Sandeepan Nath Sandeepan Nath - 5 months ago 20
PHP Question

How do I use a class method as a callback function?

If I use

inside a class function to call another function of the same class

class user
public function getUserFields($userIdsArray,$fieldsArray)

if((isNonEmptyArray($userIdsArray)) && (isNonEmptyArray($fieldsArray)))
array_walk($fieldsArray, 'test_print');

private function test_print($item, $key)
//replace the $item if it matches something


It gives me the following error -

[function.array-walk]: Unable to call
- function does not exist in ...

So, how do I specify
while using


If you want to specify a class method as a callback, you need to specify the object it belongs to:

array_walk($fieldsArray, array($this, 'test_print'));

From the manual:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Check out to see this in action.