Function object: Difference between revisions

Content deleted Content added
No edit summary
Line 566:
$a = array(3, 1, 4);
usort($a, function ($x, $y) { return $x - $y; });
</source>
 
[[PHP]] 5.3+, supports also lambda functions and closures.
 
<source lang=PHP>
function Accumulator( $start ) {
$current = $start;
return function( $x ) use( &$current ) {
return $current += $x;
};
}
</source>
 
An example of this in use:
 
<source lang=PHP>
$a = Accumulator(4);
$x = $a(5);
echo "x=$x<br/>"; // x=9
$x = $a(2);
echo "x=$x<br/>"; // x=11
</source>