当前位置:Gxlcms >
PHP教程 >
学习PHP5.3的闭包:function()use(&$param)
学习PHP5.3的闭包:function()use(&$param)
时间:2021-07-01 10:21:17
帮助过:19人阅读
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.
- ?>
|