php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64608 Anonymous Function Dereferencing
Submitted: 2013-04-07 23:09 UTC Modified: 2015-03-10 21:03 UTC
Votes:9
Avg. Score:3.1 ± 1.7
Reproduced:5 of 8 (62.5%)
Same Version:5 (100.0%)
Same OS:5 (100.0%)
From: j dot doyle133 at gmail dot com Assigned: laruence (profile)
Status: Closed Package: *General Issues
PHP Version: 5.5.0beta1 OS: All
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-08 02:48 UTC] laruence@php.net
there is already a PR about this. https://github.com/php/php-src/pull/301
 [2013-04-08 03:53 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2013-04-08 03:53 UTC] laruence@php.net
-Status: Assigned +Status: Open
 [2013-04-08 03:53 UTC] laruence@php.net
-Status: Assigned +Status: Open
 [2014-06-30 10:49 UTC] 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.
 [2015-03-10 21:03 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2015-03-10 21:03 UTC] nikic@php.net
This FR has been implemented by https://wiki.php.net/rfc/uniform_variable_syntax in PHP 7. Both f()() and (function() {})() are supported now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 19:01:30 2024 UTC