Req #64608 [Com]: Anonymous Function Dereferencing
| From: | tom at r dot je | Date: | Mon, 30 Jun 2014 10:49:53 +0000 |
| Subject: | Req #64608 [Com]: Anonymous Function Dereferencing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186388@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64608&edit=1
ID: 64608
Comment by: tom at r dot je
Reported by: j dot doyle133 at gmail dot com
Summary: Anonymous Function Dereferencing
Status: Assigned
Type: Feature/Change Request
Package: *General Issues
Operating System: All
PHP Version: 5.5.0beta1
Assigned To: laruence
Block user comment: N
Private report: N
New Comment:
Is there any update on this?
Currently, this does not work:
function f() {
return function() {
return 'foo';
};
}
echo f()();
and results in a parse error: Parse error: syntax error, unexpected '(', expecting
',' or ';'
This is fixed using
function f() {
return function() {
return 'foo';
};
}
$closure = f()
echo $closure();
but it would be less verbose if closures were treated like arrays and properly dereferenced in these
cases.
Previous Comments:
------------------------------------------------------------------------
[2013-04-08 02:48:52] laruence@php.net
there is already a PR about this. https://github.com/php/php-src/pull/301
------------------------------------------------------------------------
[2013-04-07 23:09:05] j dot doyle133 at gmail dot com
Description:
------------
The anonymous functions wouldn't really be complete without dereferencing, making
it easier for developers to split their code up into chunks, and having one return
value. Database calls that are only going to be called once for example, but need
to be stored in an array. There's no point making a whole function for it and no
point setting it to a variable to be run once.
Test script:
---------------
$my_array = [
'key_1' => function() {
return mt_rand();
}(),
'database' => function() {
$db = new PDO("mysql:host=127.0.0.1; port=3306;", "root",
"");
$sth = $db->prepare("SELECT * FROM
users WHERE username=?
LIMIT 1");
$sth->bindParam(1, 'Test');
$sth->execute();
return $sth->fetch(PDO::FETCH_ASSOC);
}()
];
Expected result:
----------------
An array with two values, a random value on 'key_1' and an associative array on
'database'.
Actual result:
--------------
Parse error: syntax error, unexpected '(', expecting ']' in php shell code on
line
4
When leaving out the parentheses; two closure classes.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64608&edit=1