1、匿名函數的概念:是指沒有函數名的方法
$func = function($param){
echo $param;
}
2、閉包是值將匿名函數當作參數傳入,也可以返回
a:function a(){
$test = function('param'){
echo 'param';
}
$test('test');
}
a();//輸出test
b:function a(){
$test = function('param'){
echo 'param';
}
return $test;
}
$test = a();
$test('test');//將匿名函數返回
c:function a($test){
$test('test');
}
$test = function('test'){
echo 'test';
}
a($test);
3、閉包函數引用外部變量需要使用use關鍵字
function a(){
$param1 = 'yes';
$param2 = 'no';
$test = function() use ($param1){
echo $param1.'\r\n';
echo $param2.'\r\n';//這裏會報錯
}
}