php 闭包 function use用法

参考: 

https://www.zhihu.com/question/28062458

https://blog.csdn.net/weixin_34219944/article/details/85523662?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.not_use_machine_learn_pai&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.not_use_machine_learn_pai

<?php
$a = 'aac';

$say = function () use (&$a) {

  echo $a;
};
$a = "zzzzzzzzzzz";
//$say();

//$fib = function($n) use(&$fib){
//   if ($n < 2) {
//     return 1;
//   }
//   return $fib($n-1) + $fib($n-2);
//};
//echo $fib(22);

function fib($n) {
  if ($n < 2) {
    return 1;
  }
  return fib($n-1) + fib($n-2);
}
$t1 = microtime(true);
var_dump(fib(40));
var_dump(fib(41));

$t2 = microtime(true);

$fib = array();
$fib[0] = 1;
$fib[1] = 1;
for ($i = 2; $i < 42; $i++) {
  $fib[$i] = $fib[$i-1] + $fib[$i-2];
}
$t3 = microtime(true);
var_dump($fib[40]);
var_dump($fib[41]);

echo "t2-t1:" . round($t2-$t1, 7)."
";
echo "t3-t2:" . round($t3-$t2, 7)."
";

  

 ----------------------------

PHP 匿名函数 function use 与直接传参的区别

https://blog.csdn.net/gezipml/article/details/83069561

<?php
$message = 'hello';
 
// 没有 "use"
$example = function () {
    var_dump($message);
};
echo $example();
 
// 继承 $message
$example = function () use ($message) {
    var_dump($message);
};
echo $example();
 
// Inherited variable's value is from when the function
// is defined, not when called
$message = 'world';
echo $example();
 
// Reset message
$message = 'hello';
 
// Inherit by-reference
$example = function () use (&$message) {
    var_dump($message);
};
echo $example();
 
// The changed value in the parent scope
// is reflected inside the function call
$message = 'world';
echo $example();
 
// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    var_dump($arg . ' ' . $message);
};
$example("hello");
?>

  

-------------------------------------------------------

解除递归函数

  1.  
    <?php
  2.  
    $fib = function($n) use(&$fib) {
  3.  
    if($n == 0 || $n == 1) return 1;
  4.  
    return $fib($n - 1) + $fib($n - 2);
  5.  
    };
  6.  
     
  7.  
    echo $fib(2) . " "; // 2
  8.  
    $lie = $fib;
  9.  
    $fib = function(){die('error');};//rewrite $fib variable
  10.  
    echo $lie(5); // error because $fib is referenced by closure

注意上题中的use使用了&,这里不使用&会出现错误$fib($n-1)是找不到function的(前面没有定义fib的类型)

所以想使用闭包解除循环函数的时候就需要使用

  1.  
    <?php
  2.  
    $recursive = function () use (&$recursive){
  3.  
    // The function is now available as $recursive
  4.  
    }

这样的形式

4 关于延迟绑定

如果你需要延迟绑定use里面的变量,你就需要使用引用,否则在定义的时候就会做一份拷贝放到use中

  1.  
    <?php
  2.  
    $result = 0;
  3.  
     
  4.  
    $one = function()
  5.  
    { var_dump($result); };
  6.  
     
  7.  
    $two = function() use ($result)
  8.  
    { var_dump($result); };
  9.  
     
  10.  
    $three = function() use (&$result)
  11.  
    { var_dump($result); };
  12.  
     
  13.  
    $result++;
  14.  
     
  15.  
    $one(); // outputs NULL: $result is not in scope
  16.  
    $two(); // outputs int(0): $result was copied
  17.  
    $three(); // outputs int(1)

使用引用和不使用引用就代表了是调用时赋值,还是申明时候赋值

原文地址:https://www.cnblogs.com/oxspirt/p/14241632.html