当前位置:Gxlcms > PHP教程 > 学习PHP5.3的闭包:function()use(&$param)

学习PHP5.3的闭包:function()use(&$param)

时间:2021-07-01 10:21:17 帮助过:19人阅读

  1. function closureCreater(){

  2. $x =1;
  3. return function($fun=null) use(&$x){//按引用传值
  4. echo "
    ".$x++;
  5. $fun and $fun();
  6. };
  7. }

  8. $x = "hello world";

  9. $test = closureCreater();
  10. $test();
  11. $test(function(){ echo "closure test one"; });
  12. $test(function(){ echo "closure test two"; });
  13. $test(function() use($x){ echo "
    ".$x;});
  14. //将函数保存为数组元素
  15. $x = 'outer param.';
  16. $arr = array();
  17. $arr[] = function($str)use($x){ return $str.$x; };
  18. echo $arr[0]('test fun in arr,'); //test fun in arr,outer param.
  19. ?>

人气教程排行