Yes, you can pass an array as second parameter, containing the object and the method name as string:
class Foo {
public function a($a) {
array_walk($a, array($this, 'b'));
}
private function b($v, $k) {
print $v;
}
}
$f = new Foo();
$f->a(array('foo', 'bar'));
prints
foobar
The signature of the array_walk()
(can be found in the documentation), defines the second argument as callback, for which the following description can be found:
A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array()
, echo()
, empty()
, eval()
, exit()
, isset()
, list()
, print()
or unset()
.
A method of an instantiated object is passed as an array
containing an object at index 0 and the method name at index 1.
Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.
Apart from common user-defined function, create_function()
can also be used to create an anonymous callback function. As of PHP 5.3.0 it is possible to also pass a closure to a callback parameter.