Alex Rivera | Logout

Why and how do you use anonymous functions in PHP?

Asked 2010-03-09T20:27:50.353
46

Anonymous functions are available from PHP 5.3.
Should I use them or avoid them? If so, how?

Edited: just found some nice trick with PHP anonymous functions:

$container           = new DependencyInjectionContainer();
$container->mail     = function($container) {};
$container->db       = function($container) {};
$container->memcache = function($container) {};
Edit
Report

1 Answer

2

A typical use of anonymous functions is callback functions. For example, you could use them for callback from sort algorithms such as in function uksort ( http://lv.php.net/uksort ) or replacing algorithms such as preg_replace_callback ( http://lv.php.net/manual/en/function.preg-replace-callback.php ). Have not tried it myself in PHP, so this is just a guess.

answered 2010-03-09T20:30:18.923

Your Answer