You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
624 B

3 years ago
  1. <?php
  2. /**
  3. * 箭头函数时匿名函数的简洁版
  4. * fn(参数)=>表达式
  5. */
  6. //匿名函数
  7. $a = function($n){
  8. return $n*$n;
  9. };
  10. echo "使用匿名函数".$a(3).PHP_EOL;
  11. //箭头函数
  12. $b=fn($n) => $n*$n;
  13. echo "使用箭头函数".$b(3).PHP_EOL;
  14. //2 箭头函数不用use,会自动继承父类的变量
  15. $c=5;
  16. //匿名函数
  17. $fn1=function() use ($c){
  18. return $c * $c;
  19. };
  20. echo $fn1().PHP_EOL;
  21. //箭头函数
  22. $fn2=fn()=>$c*$c; //这里箭头函数不用形参,就能使用父类中的$c
  23. echo $fn2().PHP_EOL;
  24. ?>