Every instance of a lambda has own instance of static variables. This provides for great event handlers, accumulators, etc., etc.
Creating new lambda with function() { ... }; expression creates new instance of its static variables. Assigning a lambda to a variable does not create a new instance. A lambda is object of class Closure, and assigning lambdas to variables has the same semantics as assigning object instance to variables.
Example script: $a and $b have separate instances of static variables, thus produce different output. However $b and $c share their instance of static variables - because $c is refers to the same object of class Closure as $b - thus produce the same output.
#!/usr/bin/env php
<?php
function generate_lambda() : Closure
{
return function($v = null) {
static $stored;
if ($v !== null)
$stored = $v;
return $stored;
};
}
$a = generate_lambda(); $b = generate_lambda(); $c = $b; $a('test AAA');
$b('test BBB');
$c('test CCC'); var_dump([ $a(), $b(), $c() ]);
?>
This test script outputs:
array(3) {
[0]=>
string(8) "test AAA"
[1]=>
string(8) "test CCC"
[2]=>
string(8) "test CCC"
}