fork download
  1. <?php
  2.  
  3. $bar = 10;
  4. $foo = 0;
  5.  
  6. $f = function() use ($bar,$foo) {
  7.  
  8. if($bar){ // condition needed to prevent infinite loop
  9. echo $bar-- . PHP_EOL;
  10. call_user_func(__FUNCTION__); // wont work
  11. }
  12. };
  13. //$f(); this will fail
  14.  
  15. echo "Function passed as argument" . PHP_EOL;
  16.  
  17. $bar = 10;
  18. $foo = 0;
  19.  
  20. $f = function( $__FUNCTION__ = false ) use (&$bar, $foo) {
  21.  
  22. if($__FUNCTION__ && $bar){
  23. echo $bar-- . PHP_EOL;
  24. call_user_func( $__FUNCTION__, $__FUNCTION__);
  25. }
  26. };
  27.  
  28. $f ( $f );
  29.  
  30.  
  31. echo "Function passed via `use' statement" . PHP_EOL;
  32.  
  33. $bar = 10;
  34. $foo = 0;
  35.  
  36. $__FUNCTION__ = function() use (&$bar, $foo, &$__FUNCTION__) {
  37.  
  38. if($bar){
  39. echo $bar-- . PHP_EOL;
  40. call_user_func( $__FUNCTION__ );
  41. }
  42. };
  43. $__FUNCTION__();
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Function passed as argument
10
9
8
7
6
5
4
3
2
1
Function passed via `use' statement
10
9
8
7
6
5
4
3
2
1