tags:

views:

489

answers:

4

The lambda anonymous function is part of PHP 5.3. What use is it? Are there some things that one can only do with lambda? Is lambda better certain for some tasks?

I've seen the Fibonacci example, and I really don't need to write Fibonacci sequences, so I'm still not sure if it's that useful for the kinds of tasks I encounter in writing webbish applications. So what does one do with it in "real life"?

+24  A: 

Anything that requires a temporary function that you probably will only use once.

I would use them for callbacks, for functions such as:

E.g.

usort($myArray, function ($a, $b) {
 return $a < $b;
});

Before 5.3, you'd have to..

function mySort ($a, $b) {
 return $a < $b;
}
usort($myArray, 'mySort');

Or create_function ...

usort($myArray, create_function('$a, $b', 'return $a < $b;'));
Matt
Thank, Matt! Could you give an example, and why it would be better than an alternative ( unless it's apparent )? :)
sure, see my update
Matt
Agreed. Many of the array processing functions use callbacks, I'd be more happy being able to use an anonymous function.
ryanday
OK, I think I'm beginning to understand. If you were to use it more than once, you'd want to create a function, because the function would only get parsed once. But if you're only going to use the function once, use a lambda, because it's less overhead than a function?
For those familiar with jQuery anonymous functions will become second nature in 5.3+
cballou
+2  A: 

There are some use cases that are made more convenient by lambdas. For instance, if a method uses a callback, using a lambda for that callback means you don't have to define a "real" function somewhere to handle it. So, lambdas keep your code cleaner.

As with the above, lambdas can be used as "short-lived" functions. For instance, sorting algorithms typically only need to know if variable a is less than variable b for any combination of a and b. To make a generic sorting algorithm that's capable of handling any class of objects, you might make your sort function definition such that it accepts a function to use as a comparator. Providing a lambda as the comparator function means that you can define your sort behavior on a per-call basis, rather than having to define a "real" function that will live for the lifetime of your script just to handle this one sorting case.

Dathan
+1  A: 

The implementation of the cryptic Y combinator?

function Y($F)
{
  $func = function ($f) { return $f($f); };

  return $func(function ($f) use($F)
  {
    return $F(function ($x) use($f)
    {
      $ff = $f($f);

      return $ff($x);
    });
  });
}

Cited Source: http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures

stormist
Well, this is cool, but it seems like it's exactly the textbook Comp Sci example that I would never use ;) What *do* you use it for, anyway?
Oh I don't use it, I cited the source where I got it from >:)
stormist
+2  A: 

Anonymous functions (closures) can be created as local functions (thus not pollluting the global space, as Dathan suggested).

With the "use" keyword, variables that are passed to or created by the enclosing function can be used inside the closure. This is very useful in callback functions that are limited in their parameter list. The "use" variables can be defined outside the closure, eliminating the need to redefine them each time the closure is called.

function change_array($arr, $pdo)
{
    $keys = array('a', 'c');
    $anon_func = function(& $val, $key) use ($keys, $pdo)
    {
         if (in_array($key, $keys) {
            $pdo->query('some query using $key');
            $val = $pdo->fetch();
        }
    }
    arr_walk($arr, $anon_func);
    return $arr;
}

$pdo = new($dsn, $uname, $pword);
$sample = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$sample = change_array($sample, $pdo);

(Of course, this example can be simpler without a closure, but it's just for demo.)

GZipp