Jump to content

PHP5.3 - Lambdas and Closures


DarkWater

Recommended Posts

Well, I recently found out that PHP5.3 is planning to have both lambdas and closures added.  I personally think this is great news, because they make one-time use functions so much easier.

 

$sort = function ($a, $b) {
        return strcmp($a, $b);
};
usort($array, $sort); //btw I know that sort() would do probably do the same thing, this was just an example

 

Opinions?

Link to comment
https://forums.phpfreaks.com/topic/130356-php53-lambdas-and-closures/
Share on other sites

Oooo nifty....  I probably won't use that much, but cool.

 

There's also closures, which basically "wrap" functions (and variables), and allow calls to the functions to modify variables within the closure.  Perl has a similar construct.

Where are we hearing this stuff?

 

I'm using 5.3 for several projects at the moment but haven't come accross anything like this.

 

Someone developed a patch for it, and apparently PHP acknowledged it.  Check out http://us.php.net/call_user_func_array

Example #3:

<?php

$func = function($arg1, $arg2) {
    return $arg1 * $arg2;
};

var_dump(call_user_func($func, 2, 4)); /* As of PHP 5.3.0 */
// int(

?>

 

EDIT: Forgot I had the patch notice bookmarked. http://news.php.net/php.internals/38290

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.