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.