Home > Backend Development > PHP Tutorial > Learn closures in PHP 5.3: function() use(&$param)

Learn closures in PHP 5.3: function() use(&$param)

WBOY
Release: 2016-07-25 09:07:42
Original
1155 people have browsed it
  1. function closureCreater(){

  2. $x =1;
  3. return function($fun=null) use(&$x){//Pass value by reference
  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. //Save the function as an array element
  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. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template