|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-04-07 23:09 UTC] 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.
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 08 17:00:01 2025 UTC |
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.