Re: [RFC] Anonymous functions

From: Date: Sat, 23 Feb 2008 18:46:34 +0000
Subject: Re: [RFC] Anonymous functions
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-35756@lists.php.net to get a copy of this message
2008/2/24, Andrei Zmievski <andrei@gravitonic.com>: > Do we have a working patch somewhere then? Hi, I have added support for closures to my patches. http://www.opendogs.org/pub/php-5.3-080223-anon.patch http://www.opendogs.org/pub/php-6.0-080223-anon.patch http://www.opendogs.org/pub/php-anon-tests-080223.tgz Example: <?php function get_counter($i = 0) { /** * 'static' modified anonymous function's * uninitialized (or null) static variables * are copied from the declared scope. */ return static function(){ static $i; return ++$i; }; } $c1 = get_counter(); $c2 = get_counter(100); var_dump($c1(), $c2()); var_dump($c1(), $c2()); var_dump($c1(), $c2()); ?> For recursive calling of anonymous functions and closures, added function get_current_function_key(). <?php $f = function($i = 0) { echo "$i "; if (++$i > 10) { return; } $f = get_current_function_key(); $f($i); }; $f(); ?> And here is a Zend extension for PHP 5.2 which enables anonymous functions, closures and square bracket arrays. http://www.opendogs.org/pub/php_qiq-0.1.0pre.tgz This has a few more features, but, at present, there is no document... Regards, -- /** * Ryusuke SEKIYAMA * rsky0711@gmail.com */

« previous php.internals (#35756) next »