Tower of Power

Too sweet to be sour.

PHP 5.3 and Closures

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
$closure = function ($args) use ($global) { /*Body*/ };

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
<?php
$global = "Global Variable";

$closureUse = function ($arg) use ($global) { echo $global . " - " . $arg; };
$closureGlobal = function ($arg) { global $global; echo $global . " - " . $arg; };

echo "Basic Closure Testsn";
echo "-------------------nn";

echo "$closureUse('test'): ";
$closureUse('test');
echo "n";

echo "$closureGlobal('test'): ";
$closureGlobal('test');
echo "n";

echo "n";
echo "Global Closure Testsn";
echo "--------------------nn";



$global = "Global Variable (Changed)";



echo "$closureUse('test') (changed $global): ";
$closureUse('test');
echo "n";

echo "$closureGlobal('test') (changed $global): ";
$closureGlobal('test');
echo "n";

Would output:

1
2
3
4
5
6
7
8
9
10
11
Basic Closure Tests
-------------------

$closureUse('test'): Global Variable - test
$closureGlobal('test'): Global Variable - test

Global Closure Tests
--------------------

$closureUse('test') (changed $global): Global Variable - test
$closureGlobal('test') (changed $global): Global Variable (Changed) - test

You can, as well, return references with a closure by putting the & between the function keyword and the parenthesis.

1
$closureReturnsReference = function & ($args) use ($global) { /*Body*/ };

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.

Comments