Anonymous functions in PHP

I ran into this and found it interesting. Someone has added support for anonymous functions in PHP.

With the patch you can now do stuff like:

[code lang=”php”]
$data = array(“zoo”, “orange”, “car”, “lemon”, “apple”);
usort($data, function($a, $b) { return strcmp($a, $b); });
var_dump($data); # data is sorted alphabetically

Before you had to use a funky function generation call.


3 thoughts on “Anonymous functions in PHP

  1. SantosJ

    I stopped following the discussion after a while (and any discussion on the Internals Mailing List). I’m more interested in if they are going to have it in the next version of PHP. The patch is easy enough that you can add it yourself.

    It is good that people with more karma was able to come in and support the idea.

  2. Edward Z. Yang

    Ah, that sounds absolutely delightful! I’ve always found JavaScript’s first-class treatment of anonymous functions quite helpful, it’s nice to see it come into PHP now.

Comments are closed.