function closureCreater(){ - $x =1;
- return function($fun=null) use(&$x){//按引用传值
- echo "
".$x++; - $fun and $fun();
- };
- }
$x = "hello world"; - $test = closureCreater();
- $test();
- $test(function(){ echo "closure test one"; });
- $test(function(){ echo "closure test two"; });
- $test(function() use($x){ echo "
".$x;}); -
- //将函数保存为数组元素
- $x = 'outer param.';
- $arr = array();
- $arr[] = function($str)use($x){ return $str.$x; };
- echo $arr[0]('test fun in arr,'); //test fun in arr,outer param.
- ?>
|