Our blog

 

PHP Calling Methods using Array_Walk

The array_walk function is one of those unassuming little PHP functions that a lot of people may well ignore, but it is often very useful and can save writing out lines of code, compressing a pretty common coding structure into a single function call. Of course that also means that it is fast!

However, the standard documentation doesn't make it clear if you can use array_walk on an array and use a class method as the callback rather than a global function. You can and here is how to do it:

PHP:
  1. $object = new MyClass;
  2. $array = array(/* some array */);
  3. array_walk($array, array($object, 'methodName'));

More Reading:

One Comments

admin
May 14th, 2010

note - within the class context, simply use $this rather than $object ;)

 

 

Leave a Reply