php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52630 additional syntax for closures
Submitted: 2010-08-17 22:17 UTC Modified: 2015-03-10 21:18 UTC
Votes:5
Avg. Score:4.6 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: giorgio dot liscio at email dot it Assigned: stas (profile)
Status: Closed Package: Variables related
PHP Version: 5.3.3 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
40 + 26 = ?
Subscribe to this entry?

 
 [2010-08-17 22:17 UTC] giorgio dot liscio at email dot it
Description:
------------
hi, would be nice having this syntax for closures


$this->addResult
(
    (
        function ($param) use ($x) { return $x*$param; }
    )($result)
);

a closure defined and executed in the same instruction, as JavaScript allows



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-17 22:21 UTC] kalle@php.net
-Assigned To: +Assigned To: stas
 [2010-08-17 22:21 UTC] kalle@php.net
I belive stas' had a patch for this? Or atleast function call chaining, but I assume if implemented it would allow this syntax?
 [2010-08-17 22:41 UTC] giorgio dot liscio at email dot it
any alternative syntax can be good if the result is the same, defining end invoking on the same instruction without using any var

now:

$f = function($x) use($y){return $x * $y;};
$f(22);

request:

(function($x) use($y) {return $x * $y;})(22);

or

function($x) use($y) {return $x * $y;}(22);

or 

function($x) use($y) invoke(22) {return $x * $y;};

etc
 [2010-08-18 01:02 UTC] kalle@php.net
I like the:
function($x) use($y) {return $x * $y;}(22);

approach, its consistent with that of other languages, like JavaScript
 [2010-09-02 00:06 UTC] stas@php.net
-Status: Assigned +Status: Open -Type: Bug +Type: Feature/Change Request
 [2015-03-10 21:18 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2015-03-10 21:18 UTC] nikic@php.net
Implemented by https://wiki.php.net/rfc/uniform_variable_syntax in PHP 7. Parentheses around the closure are required.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Jan 28 19:01:26 2020 UTC