Anonymous (Lambda) Functions in PHP.

I’ve recently been playing with anonymous (lambda) functions in javascript and I was thinking that it would be great if I could do the same thing in php, which is what I spend most of my time with. Turns out that you can. It’s not as clean and pretty as with javascript but it’s still a lambda function and functional programmers are better…aren’t they?

First the spec :
string create_function ( string $args , string $code )

So here’s a little example of something completely useless that can be done using anonymous functions in PHP :

$first_arg = 100;
$some_function = create_function(‘$first_arg’,’return $first_arg/5;’);

function process($arg,$func)
{
    while($func($arg)>5)
    {
        $arg–;
    }
    return $arg;
}

echo process($first_arg,$some_function);

So we get 25 echoed to the screen. A bunch of better examples can be found at the php website. If you’re willing to read…

Nothing really special but It’s still cool that can pass functions into another PHP function just like a regular variable.


Share

11 Responses to “Anonymous (Lambda) Functions in PHP.”

  • Josh Butts Says:

    create_function can be nice, but the underpinnings of it in at the language level are a bit nasty. PHP 5.3 will introduce real closures and lambda functions, which should make the syntax and support for this sort of thing way better. Or worse.

  • Wesley Walser Says:

    I think you may want to look here ->
    http://us3.php.net/manual/en/functions.anonymous.php

    This is the lambda that most people are talking about when it comes to php as it’s going to be available in 5.3. The syntax is much nicer than create_function and is what most people expect when they use a lambda function.

    create_function creates code so messy and unreadable I don’t really think I would want anyone on my team using it.

  • Eugene Kirpichov Says:

    It is not a lambda because it can’t use variables from its defining scope. This way, anonymous functions are almost completely useless.

  • Mason Says:

    The shortcut is just to use strings.

    $str = ” string surrounded by whitespace “;
    $f = “trim”;
    echo $f($str);

    Not as cool as having a function pointer, but useful.

  • Mason Says:

    Oh… I guess you were more interested passing a full function around, not a handle. Ignore me. :)

  • abcphp.com Says:

    Anonymous (Lambda) Functions in PHP. | tech stuff from cody taylor…

    I’ve recently been playing with anonymous (lambda) functions in javascript and I was thinking that it would be great if I could do the same thing in php, which is what I spend most of my time with. Turns out that you can. It’s not as clean and pret…

  • Josho Says:

    Actually, you still can’t pass functions around like that. create_function adds a function to the current execution, but gives it a random, unused name and returns a string name. String-based function finding has been a feature for a while –

  • Stuart Loxton Says:

    In PHP 5.3 you can use lamdas exactly the same as JS:

    process(100, function($first_arg) {
    return $first_arg / 5;
    });

    Much prettier.

  • Balaji Says:

    Have you given any thoughts of using this with PHP.JS? I’ve been hard pressed to find good resources, and judging from this article I’m guessing you may have something valuable to say. Thanks in advance!

  • Matt Butcher Says:

    @Stuart Loxton
    PHP 5.3 will also support (explicit) closures. Many times, JavaScripters use functions to create closures. While this won’t work with PHP 5.3 anonymous functions, PHP closures will provide something analogous to JavaScript.

  • ellisgl Says:

    @Balaji
    Next thing you know, there will be a PHP.jQuery…