Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X
Post

Using PHP callback functions in array_walk in a class.

Ran into something small that might be helpful for other PHP types.  This isn’t rocket science, but I don’t see enough examples with static function calls for these functions.

array_walk lets you call a function that will be iterated over all members of an array.  In my template class, I had to append some text to the front and back of a search variable.  Instead of using the poorly performing foreach loop, I just ran the array_walk function and passed it a function that did the same thing.

array_walk($search, 'template_variable');

I decided that this was not what I wanted (since template_variable would be defined as a function in the global context), and I preferred putting related functions in the same class.  I tried this:

array_walk($search, 'self::template_variable');

Of course, this didn’t work.  array_walk is not a member of the Template class and as a result it just threw an error stating that “self::template_variable” didn’t exist.

array_walk($search, 'Template::template_variable');

This works fine.

-edit-

There’s something weird and inconsistent about all of this.  template_variable works even while private, which throws everything out of wack.