Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.
Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-anon.patch
Featues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars . - Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.
Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6
// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"
// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>
The anonymous function name is composed of '\0', "ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
. - "ZEND_ANON<" helps determine wheter the function is anonymous or not
because '<' cannot be used for user-defined function name. - Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.
Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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
--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/
Hello Ryusuke,
+1, looks pretty nice, makes my life easier in some cases and is closer to
other languages like pythin so i can transfer concepts easily which is a
big plus for me.
marcus
Thursday, January 10, 2008, 11:09:26 AM, you wrote:
Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure.
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-anon.patch
Featues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.
Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6
// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"
// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>>
The anonymous function name is composed of '\0', "ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.
Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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
--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/
Best regards,
Marcus
Hello Ryusuke,
+1, looks pretty nice, makes my life easier in some cases and is closer to
other languages like pythin so i can transfer concepts easily which is a
big plus for me.marcus
Thursday, January 10, 2008, 11:09:26 AM, you wrote:
Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure.
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>>The anonymous function name is composed of '\0', "ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/Best regards,
Marcus--
A nice feature. Especially for array callbacks.
--
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>The anonymous function name is composed of '\0', "ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/--
KOYAMA, Tetsuji
koyama@hoge.org
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.
Thanks,
Mat
KOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>The anonymous function name is composed of '\0', "ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/--
KOYAMA, Tetsuji
koyama@hoge.org
Yes, let's re-open, I'd like to get a closure on that issue (pun
intended).
-Andrei
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.Thanks,
MatKOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-
anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-
anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>The anonymous function name is composed of '\0',
"ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous
or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/--
KOYAMA, Tetsuji
koyama@hoge.org
Hello Andrei,
+1
Friday, February 22, 2008, 7:00:11 PM, you wrote:
Yes, let's re-open, I'd like to get a closure on that issue (pun
intended).
-Andrei
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.Thanks,
MatKOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
[...]
2008/1/6, Marcus Boerger helly@php.net:
[...]
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
Best regards,
Marcus
And +1 from me :)
David
Am 23.02.2008 um 12:43 schrieb Marcus Boerger:
Hello Andrei,
+1
Friday, February 22, 2008, 7:00:11 PM, you wrote:
Yes, let's re-open, I'd like to get a closure on that issue (pun
intended).-Andrei
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.Thanks,
MatKOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
[...]
2008/1/6, Marcus Boerger helly@php.net:
[...]
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
Best regards,
Marcus
Do we have a working patch somewhere then?
-Andrei
Hello Andrei,
+1
Friday, February 22, 2008, 7:00:11 PM, you wrote:
Yes, let's re-open, I'd like to get a closure on that issue (pun
intended).-Andrei
Do we have a working patch somewhere then?
Did we not get one from Wez? and an alternative from <I don't remember> :)
Am 23.02.2008 um 17:13 schrieb Pierre Joye:
On Sat, Feb 23, 2008 at 5:09 PM, Andrei Zmievski <andrei@gravitonic.com
wrote:
Do we have a working patch somewhere then?Did we not get one from Wez? and an alternative from <I don't
remember> :)
http://news.php.net/php.internals/34216
I think the last version should be http://www.christian-seiler.de/temp/closures-php-5-3-v2.patch
. AFAIK he has planned to improve the patch from user feedback, but
I'm not exactly sure about that.
Tobias
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
*/
Hello Ryusuke,
I have put your proposal as a link to a PHP GSoC 2008 idea here:
http://wiki.php.net/gsoc/2008
Feel invited to add to this idea in whatever way you want :-)
marcus
Saturday, February 23, 2008, 7:46:34 PM, you wrote:
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.
This has a few more features, but, at present, there is
no document...
Regards,
Best regards,
Marcus
Hello Ryusuke,
I have put your proposal as a link to a PHP GSoC 2008 idea here:
http://wiki.php.net/gsoc/2008Feel invited to add to this idea in whatever way you want :-)
how is it related to gsoc?
patches are already there — all we need is to decide which one is
better, and what needs to be fixed. no?
marcus
Saturday, February 23, 2008, 7:46:34 PM, you wrote:
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.tgzExample:
<?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.This has a few more features, but, at present, there is
no document...Regards,
Best regards,
Marcus
--
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
Hello Alexey,
my last update on this was that people were unhappy with closures. If
that is not the case than maybe we need to write this as an RFC on the
wiki: http://wiki.php.net/rfc
So well maybe you're right and it only requires selecting between the two
and checking whether there are tiny parts missing.
marcus
Monday, March 24, 2008, 10:21:57 AM, you wrote:
Hello Ryusuke,
I have put your proposal as a link to a PHP GSoC 2008 idea here:
http://wiki.php.net/gsoc/2008Feel invited to add to this idea in whatever way you want :-)
how is it related to gsoc?
patches are already there — all we need is to decide which one is
better, and what needs to be fixed. no?
marcus
Saturday, February 23, 2008, 7:46:34 PM, you wrote:
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.tgzExample:
<?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.This has a few more features, but, at present, there is
no document...Regards,
Best regards,
Marcus
--
Best regards,
Marcus
Hi Andrei,
Am Freitag, den 22.02.2008, 10:00 -0800 schrieb Andrei Zmievski:
Yes, let's re-open, I'd like to get a closure on that issue (pun
intended).
+1. I would love it.
cu, Lars
Lars Strojny
Senior Software Developer Media Ventures GmbH (http://mediaventures.de)
I think we have to be very clear whether we are going to provide just a
sexier notation for anonymous functions or closures (and if the latter
what the semantics are).
Just doing one and not figuring out the long term piece doesn't make
sense because we may end up having two completely distinct features
and/or feature creep over the next year which doesn't make much sense to
me.
I think in last discussion most ppl just preferred a sexier anonymous
function esp. as there weren't very clear/clean proposals for how
closures would work in PHP and what the benefits would be.
So I definitely suggest to have a discussion, maybe RFC based like
Stefan did but really decide once and for all on where we'd like to go
with this. We all know that no features map 1:1 to PHP and there are
always unique behaviors of our language and environment which require a
fresh view at things.
Andi
-----Original Message-----
From: Andrei Zmievski [mailto:andrei@gravitonic.com]
Sent: Friday, February 22, 2008 10:00 AM
To: Matvey Arye
Cc: KOYAMA Tetsuji; internals@lists.php.net
Subject: Re: [PHP-DEV] [RFC] Anonymous functionsYes, let's re-open, I'd like to get a closure on that issue (pun
intended).-Andrei
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.Thanks,
MatKOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
On Jan 10, 2008 7:09 PM, Ryusuke SEKIYAMA rsky0711@gmail.com
wrote:Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-
anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-
anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>The anonymous function name is composed of '\0',
"ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous
or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/--
KOYAMA, Tetsuji
koyama@hoge.org
To be clear, my statement was about the closures and not sexier
anonymous functions. I agree that we need to do an RFC on this matter.
-Andrei
I think we have to be very clear whether we are going to provide
just a
sexier notation for anonymous functions or closures (and if the latter
what the semantics are).
Just doing one and not figuring out the long term piece doesn't make
sense because we may end up having two completely distinct features
and/or feature creep over the next year which doesn't make much
sense to
me.I think in last discussion most ppl just preferred a sexier anonymous
function esp. as there weren't very clear/clean proposals for how
closures would work in PHP and what the benefits would be.So I definitely suggest to have a discussion, maybe RFC based like
Stefan did but really decide once and for all on where we'd like to go
with this. We all know that no features map 1:1 to PHP and there are
always unique behaviors of our language and environment which
require a
fresh view at things.
Hi!
To be clear, my statement was about the closures and not sexier
anonymous functions. I agree that we need to do an RFC on this matter.
I'm curious - for which PHP version all these things are targeted?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Whichever one the newly developed features are going into these days.
Seems to be 5.3 or 5.4.
-Andrei
Hi!
To be clear, my statement was about the closures and not sexier
anonymous functions. I agree that we need to do an RFC on this
matter.I'm curious - for which PHP version all these things are targeted?
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Hello Andi,
my understanding was that we did not like the anon function syntax sugar
and could not agree on a feature set for closures upfront and thus decided
to disagree and not do anything. Now that the topic came up again it seems
that we actually should work on an agreeable featureset in a form of a RFC.
My current opinion is that we should have a local scope created on anon
function creation that would even contain $this. Now this is even more so
premature so I lea ve it for a RFC.
What we need is someone to start that RFC, any volunteers?
marcus
Saturday, February 23, 2008, 8:36:18 PM, you wrote:
I think we have to be very clear whether we are going to provide just a
sexier notation for anonymous functions or closures (and if the latter
what the semantics are).
Just doing one and not figuring out the long term piece doesn't make
sense because we may end up having two completely distinct features
and/or feature creep over the next year which doesn't make much sense to
me.
I think in last discussion most ppl just preferred a sexier anonymous
function esp. as there weren't very clear/clean proposals for how
closures would work in PHP and what the benefits would be.
So I definitely suggest to have a discussion, maybe RFC based like
Stefan did but really decide once and for all on where we'd like to go
with this. We all know that no features map 1:1 to PHP and there are
always unique behaviors of our language and environment which require a
fresh view at things.
Andi
-----Original Message-----
From: Andrei Zmievski [mailto:andrei@gravitonic.com]
Sent: Friday, February 22, 2008 10:00 AM
To: Matvey Arye
Cc: KOYAMA Tetsuji; internals@lists.php.net
Subject: Re: [PHP-DEV] [RFC] Anonymous functionsYes, let's re-open, I'd like to get a closure on that issue (pun
intended).-Andrei
Hi All,
I second this. Can we please re-open the discussion on anonymous
functions as well as closures. That would be an awesome feature.
create_function is a bit ugly semantically to be sufficient for all
anonymous function needs.Thanks,
MatKOYAMA Tetsuji wrote:
Hi lists,
Is this discussion stopping?
I want this feature in PHP. Please start to talk.
On Jan 10, 2008 7:09 PM, Ryusuke SEKIYAMA rsky0711@gmail.com
wrote:Hello, lists,
We have discussed about implementing anonymous functions and
closures in PHP.
However, I consider that implementing anonymous functions and
implementing lexical scopes should be discussed separately.Even though I like closure. ;-)
So I wrote anonymous function patch for PHP 5.3 and 6.0.
Patches (inlcude tests):
for PHP 5.3: http://www.opendogs.org/pub/php-5.3dev-080109-
anon.patch
for PHP 6.0: http://www.opendogs.org/pub/php-6.0dev-080109-
anon.patchFeatues:
- Unlike
create_function()
, there is no need to take care of
quotes, backslashes and dollars .- Can be used in loop, but be compiled only once.
- Supports recursive call using FUNCTION.
- Supports inline execution. It works like a block scope.
- Works with opcode caches.
Since I couldn't build APC against PHP 5.3, I have tested
PHP 5.2.5 + anonymous function patch and APC 3.0.16.Example:
<?php
// callback
$arr = array(5, 3, 6, 0);
usort($arr, function($a, $b){ return $a - $b; });
print_r($arr); // 0, 3, 5, 6// assign to a variable
$lambda = function(){
echo "Hello, World!\n";
};
$lambda(); // "Hello, World!"// inline execution
$result = function($a, $b){
return $a + $b;
}(1, 2);
var_dump($result); // "int(3)"
?>The anonymous function name is composed of '\0',
"ZEND_ANON<#serial>",
filename and character position.
- Leading '\0' blocks to be displayed by
get_defined_functions()
.- "ZEND_ANON<" helps determine wheter the function is anonymous
or not
because '<' cannot be used for user-defined function name.- Serial number and file informations make the name unique.
- char anon_key_buf[32] is long enough because
`snprintf(anon_key_buf, 32, "ZEND_ANON<%llu>", (unsigned long
long)UINT64_MAX)'
returns 31.Regards,
2008/1/6, Marcus Boerger helly@php.net:
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:
the square bracket array syntax patch for PHP 5.3,
http://www.opendogs.org/pub/php-5.3dev-080101-sbar.patchI 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--
/**
- Ryusuke SEKIYAMA
- rsky0711@gmail.com
*/--
KOYAMA, Tetsuji
koyama@hoge.org--
--
Best regards,
Marcus