# PATCH: Implementing closures in PHP

- Thread: https://externals.io/message/34272
- Original archive: https://news-web.php.net/php.internals/34272
- Started by Ryusuke SEKIYAMA on 2007-12-28T16:45:03+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #34272 — Ryusuke SEKIYAMA — 2007-12-28T16:45:03+00:00

URL: https://externals.io/message/34272#34272

>any idea about the possibility of hash conflict?

How about this patch?
http://www.opendogs.org/pub/php-5.3dev-071228a.patch

This patch is PHP 5.3 version of the patch I have introduced
in my blog. (written in Japanese)
1. http://d.hatena.ne.jp/rsky/20071126/1196077860
2. http://d.hatena.ne.jp/rsky/20071128/1196260732
3. http://d.hatena.ne.jp/rsky/20071130/1196426672

This patch contains two features, anonymous function and
square bracket array syntax.

Anonymous function can be used in loop (does not exhaust memory
as create_function() in loop), work with opcode caches
and also can be called directly like JavaScript's one.

For example:
% php -r 'print_r(array_map(function($n){ return $n * $n; }, [1, 2, 3]));'
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
)
% php -r 'function($str){ echo $str, "\n"; }("foo");'
foo

More examples are included in this archive:
http://www.opendogs.org/pub/php-5.3dev-071228-anon-patch.zip

-- 
/**
 * Ryusuke SEKIYAMA
 * rsky0711@gmail.com
 */

---

## #34277 — Xuefer — 2007-12-30T09:45:45+00:00

URL: https://externals.io/message/34272#34277 · In reply to: #34272

> > […]
>
> How about this patch?
> http://www.opendogs.org/pub/php-5.3dev-071228a.patch
yeah build_runtime_defined_function_key! that's what i thought of :)
and i hope the function name cv works as expected so no `hash()` on the
looong name is needed at run time, and assume that modern cpu will do
long memcmp/strcmp quick enough

---

## #34305 — Marcus Boerger — 2007-12-30T18:09:49+00:00

URL: https://externals.io/message/34272#34305 · In reply to: #34272

Hello Ryusuke,

  generally I like both. But could you provide separate patches?

marcus

Friday, December 28, 2007, 5:45:03 PM, you wrote:

> > […]

> How about this patch?
> http://www.opendogs.org/pub/php-5.3dev-071228a.patch

> This patch is PHP 5.3 version of the patch I have introduced
> in my blog. (written in Japanese)
> 1. http://d.hatena.ne.jp/rsky/20071126/1196077860
> 2. http://d.hatena.ne.jp/rsky/20071128/1196260732
> 3. http://d.hatena.ne.jp/rsky/20071130/1196426672

> This patch contains two features, anonymous function and
> square bracket array syntax.

> Anonymous function can be used in loop (does not exhaust memory
> as create_function() in loop), work with opcode caches
> and also can be called directly like JavaScript's one.

> For example:
> % php -r 'print_r(array_map(function($n){ return $n * $n; }, [1, 2, 3]));'
> Array
> (
>     [0] => 1
>     [1] => 4
>     [2] => 9
> )
> % php -r 'function($str){ echo $str, "\n"; }("foo");'
> foo

> More examples are included in this archive:
> http://www.opendogs.org/pub/php-5.3dev-071228-anon-patch.zip

> -- 
> /**
>  * Ryusuke SEKIYAMA
>  * rsky0711@gmail.com
>  */




Best regards,
 Marcus

---

## #34318 — Ryusuke SEKIYAMA — 2008-01-02T05:49:26+00:00

URL: https://externals.io/message/34272#34318 · In reply to: #34305

Hi, Marcus.

2007/12/31, Marcus Boerger <helly@php.net>:
> Hello Ryusuke,
>
>   generally I like both. But could you provide separate patches?

Yes, I can. Here is the anonymous function patch for PHP 5.3,
  http://www.opendogs.org/pub/php-5.3dev-080101-anon.patch
the square bracket array syntax patch for PHP 5.3,
  http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patch
and the anonymous function patch for PHP 6.0.
  http://www.opendogs.org/pub/php-6.0dev-080101-anon.patch


Regards.

-- 
/**
 * Ryusuke SEKIYAMA
 * rsky0711@gmail.com
 */

---

## #34319 — Stanislav Malyshev — 2008-01-02T18:51:06+00:00

URL: https://externals.io/message/34272#34319 · In reply to: #34318

> the square bracket array syntax patch for PHP 5.3,
>   http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patch

I remember we discussed that already and it was rejected then (even 
though myself and Andi liked it) - did the people that objected then 
change their minds?
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #34527 — Marcus Boerger — 2008-01-06T14:17:48+00:00

URL: https://externals.io/message/34272#34527 · In reply to: #34319

Hello Stanislav,

  tha makesw three then already, how about we ask around again?
Ryusuke, can you please start a new '[RFC] Square brackets shortcut' thread
to collect opinions and pass along the patch for that?

I like the anonymous function patch too. It is clean and simple. Maybe you
want to start a second '[RFC] Anonymous functions' thread with that patch.

Can you also please add tests for both?

marcus

Wednesday, January 2, 2008, 7:51:06 PM, you wrote:

> > […]

> I remember we discussed that already and it was rejected then (even 
> though myself and Andi liked it) - did the people that objected then 
> change their minds?



Best regards,
 Marcus
