I’m fairly excited because as of only a week or so ago I found the closures rfc, and not only did I find that the rfc has a working patch, apparently it’s already in PHP 5.3.
You see, apparently it’s not good enough that we get name spaces and late static binding, oh no, we get closures too!
I downloaded the Win32 snapshot from PHP snapshots page and indeed, closure support was included.
1
|
|
According to the rfc, one must manually define external variables used within a function, however in my own tests you can still use the global
keyword. The difference between the two is the use
statement preserves the value of $global
at creation and the global
keyword would work as you would expect it to with a normal function. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Would output:
1 2 3 4 5 6 7 8 9 10 11 |
|
You can, as well, return references with a closure by putting the & between the function keyword and the parenthesis.
1
|
|
Just one of many shiny fancy things to look forward to in PHP 5.3.
Edit 7/23/2008:
I should mention to the people that trashed my examples on reddit, I’m sorry for assuming you knew what closures were and the the real world uses of them. I will try to refrain from giving a concise example of how a new language feature interacts with existing features and conventions, especially in PHP where things are a bit disorganized.