[wp-hackers] anonymous functions for do_action and apply_filters

Nathaniel Taintor goldenapplesdesign at gmail.com
Mon Dec 20 20:49:05 UTC 2010


I think its generally frowned upon, because if you add an anonymous
function, then you have no way of removing it, other than calling
remove_all_filters.

There are also concerns about memory leaks caused by using lambda functions
in php. The add_filter code you cited would create a new function every time
it was called, which may not be a big deal in your case (you're not likely
to trigger the title filter more than 20-50 times in any one page), but
would still be slightly less efficient than defining the function once.



Nathaniel Taintor, Designer/Developer
*Golden Apples Design*
http://goldenapplesdesign.com

@GoldenApples | 503.877.2880
goldenapplesdesign at gmail.com



On Mon, Dec 20, 2010 at 12:43 PM, Leo germani <leogermani at gmail.com> wrote:

> Hi there,
>
> have any of you guys discussed about adding the possibility to use php
> anonymous functions [1] with the hooks?
>
> I mean something like:
>
> add_filter('the_title', function($title) { return '<b>'. $title. '</b>';
> });
>
> instead of declaring a function and passing its name as a paramenter to
> add_filter and add_action.
>
> What you think about it?
>
> Leo,,
>
> [1] http://php.net/manual/en/functions.anonymous.php
> --
>
>


More information about the wp-hackers mailing list