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
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:
7 + 36 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC