Req #64608 [Asn->Csd]: Anonymous Function Dereferencing
| From: | nikic@php.net | Date: | Tue, 10 Mar 2015 21:03:05 +0000 |
| Subject: | Req #64608 [Asn->Csd]: Anonymous Function Dereferencing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-191294@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
Updated by: nikic@php.net
Reported by: j dot doyle133 at gmail dot com
Summary: Anonymous Function Dereferencing
-Status: Assigned
+Status: Closed
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:
This FR has been implemented by https://wiki.php.net/rfc/uniform_variable_syntax
in PHP 7. Both f()() and (function() {})() are supported now.
Previous Comments:
------------------------------------------------------------------------
[2014-06-30 10:49:52] tom at r dot je
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.
------------------------------------------------------------------------
[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