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.
33 lines
624 B
33 lines
624 B
<?php
|
|
/**
|
|
* 箭头函数时匿名函数的简洁版
|
|
* fn(参数)=>表达式
|
|
*/
|
|
|
|
//匿名函数
|
|
$a = function($n){
|
|
return $n*$n;
|
|
};
|
|
|
|
echo "使用匿名函数".$a(3).PHP_EOL;
|
|
|
|
//箭头函数
|
|
$b=fn($n) => $n*$n;
|
|
echo "使用箭头函数".$b(3).PHP_EOL;
|
|
|
|
|
|
//2 箭头函数不用use,会自动继承父类的变量
|
|
$c=5;
|
|
|
|
//匿名函数
|
|
$fn1=function() use ($c){
|
|
return $c * $c;
|
|
};
|
|
|
|
echo $fn1().PHP_EOL;
|
|
|
|
//箭头函数
|
|
$fn2=fn()=>$c*$c; //这里箭头函数不用形参,就能使用父类中的$c
|
|
echo $fn2().PHP_EOL;
|
|
|
|
?>
|