Array_walk using using function inside a class?

Hello,
How to call functions which are inside classes as a parameter of array_walk? I have tried plain “functionname” and it throws an error, that the function does not exist.

Thanks for help

Depends on the class type:

If the class is static:
array_walk($array, array(‘self’, ‘walkFunction’));
or
array_walk($array, array(‘className’, ‘walkFunction’));

Otherwise:
array_walk($array, array($this, ‘walkFunction’));

Thanks, it is strange there is no word about that in neither reference nor its discussion.

At [http://www.php.net/array_walk, the function signature is:

bool array_walk ( array &$array, callback $funcname [, mixed $userdata] )

At the end of that page, there is a reference to the [url=http://www.php.net/callback]callback type](http://www.php.net/array_walk).

There is a comment which discusses using callbacks too (though I think the person who posted it is wrong about the array(&$this, ‘method’) nomenclature).

One can call a class method anonymously using the call_user_func_array() method (see the page about callbacks). This has been cleverly used to implement proxy objects and lazy loading in the WACT framework, whose main author lurks in the PHP Application Design forum under the name Selkirk. As I recall, Jason Sweat, who also contributes to the forum, has used it in his book about Design Patterns.

If you use PHP4, the comment is valid. For PHP5, you should just use Array($this, ‘someMethod’) as you already suggested.

It’s been a while since I used an ampersand! (Not missing PHP4 at all.)