先理解三个概念
回调函数:
自己编写,单不是自己调用,是别人来调用的函数;例如:call_user_func_array()、array_map();匿名函数:
没有函数名的函数,例如:<?php declare(strict_types = 1);
$no = function (int $a): int
{
return a++;
};
$no(1);
闭包:
代码角度讲,就是函数内部使用匿名函数 + use 变量,就是闭包(因为不使用 use 的匿名函数完全可以用普通函数完成)。例子:<?php declare(strict_types = 1);
function func (int $a, int $b): int
{
$c = 5;
function (int $c) use ($a, $b): int
{
return $a + $b + c;
}
}