php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54888 The use of "as" in closure "use"
Submitted: 2011-05-20 13:47 UTC Modified: 2018-05-05 21:03 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: alex at arcomit dot co dot uk Assigned: requinix (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.6 OS:
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: alex at arcomit dot co dot uk
New email:
PHP Version: OS:

 

 [2011-05-20 13:47 UTC] alex at arcomit dot co dot uk
Description:
------------
Would be handy if "as" or something similar could be used in closures to allow for easy passing of class properties.

e.g.

$callback = function() use ($this as $_this) {

};

$callback = function() use ($this->prop1 as $prop) {

};


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-05 21:03 UTC] requinix@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: requinix
 [2018-05-05 21:03 UTC] requinix@php.net
As of PHP 5.4 closures inherit $this scope from where they were defined, meaning you can use $this directly without use-ing anything.
https://3v4l.org/oM0Pc

I anyone has other use cases, beyond just "I want a different variable name", then we can reopen.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 15:01:30 2025 UTC