php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61682 allow use( $var as $varCopy, &$var as $varRef) syntax for closures
Submitted: 2012-04-09 15:41 UTC Modified: 2021-07-09 13:38 UTC
From: ninzya at inbox dot lv Assigned: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.4.1RC1 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ninzya at inbox dot lv
New email:
PHP Version: OS:

 

 [2012-04-09 15:41 UTC] ninzya at inbox dot lv
Description:
------------
I'd like to propose a syntax for closures that would allow to specify a name for 
the imported symbol (variable). Please see test script.

Test script:
---------------
$someLongArray =array( 1, 2, 3 );

$myFn =function() use( $someLongArray as $arrCopy, &$someLongArray as $arrRef ) {
  array_push( $arrCopy, 4);
  array_push( $arrRef, 5);
};

print_r( $someLongArray);// 1, 2, 3, 5

Expected result:
----------------
Expecting such syntax to be supported in the future.

Actual result:
--------------
Syntax error.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-09 15:43 UTC] ninzya at inbox dot lv
I'm sorry, in the test script a $myFn() call should follow $myFn definition:

$someLongArray =array( 1, 2, 3 );

$myFn =function() use( $someLongArray as $arrCopy, &$someLongArray as $arrRef ) 
{
  array_push( $arrCopy, 4);
  array_push( $arrRef, 5);
};

$myFn();

print_r( $someLongArray);// 1, 2, 3, 5
 [2012-04-12 15:34 UTC] laruence@php.net
I have made a RFC for this, but since it's a new feature, it will take time to 
discuss :) 

https://wiki.php.net/rfc/useas
 [2012-04-13 04:18 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2017-10-24 07:58 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: laruence +Assigned To:
 [2021-07-09 13:38 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-07-09 13:38 UTC] cmb@php.net
The RFC has been withdrawn years ago, and there has been some
opposition on the mailing list thread regarding that feature[1].
Thus I'm closing this ticket as WONTFIX.

Anyone who is still interested in the feature, please pursue the
RFC process[2].

[1] <https://externals.io/message/61404>
[2] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 02:01:36 2025 UTC