Are there plans to allow anonymous functions as callbacks?
| From: | Richard Quadling | Date: | Wed, 04 Mar 2009 11:33:32 +0000 |
| Subject: | Are there plans to allow anonymous functions as callbacks? | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-43240@lists.php.net to get a copy of this message | ||
Hi.
Quite a simple question (assuming I've got the terminology correct).
Are there any plans to allow code like this (amended example taken
from the array_map() documentation) ...
<?php
$a = array(1, 2, 3, 4, 5);
$b = array_map(function($n){return($n * $n * $n)}, $a);
print_r($b);
For me, this is cleaner than having a use-once, namespace-polluting function.
Currently, this is producing a parse error.
A real alternative though would be to allow nested functions...
<?php
function cube_array($a) {
function cube($n) {
return($n * $n * $n);
}
return array_map("cube", $a);
}
$a = array(1, 2, 3, 4, 5);
$b = cube_array($a);
print_r($b);
Ideally, cube() would not be visible outside of cube_array(). Scoped functions.
Regards,
Richard Quadling.
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"