rob*_*895 1 php anonymous-function
在这个例子中:
$foo = function () use (){
// ^---------^ what is the use?
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我们可以使用该use ()部件将数据导入到函数中,就像常规函数一样:
function bar ($foo){
}
Run Code Online (Sandbox Code Playgroud)
但我不明白这function ()部分可以用于什么?
例如:
$a = 1;
$foo = function ($b, $c) use ($a)
{
echo $a . PHP_EOL;
echo $b . PHP_EOL;
echo $c . PHP_EOL;
};
$foo(2, 3);
$a = 5;
$foo(7, 8);
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
1
7
8
Run Code Online (Sandbox Code Playgroud)
$ b,$ c - 将是您执行时传递的参数. $ a在创建时绑定/导入.因此,如果您在$ foo定义后更改它,它将不会更改.
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |