匿名函数,也叫闭包函数(closures)允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然也有其它应用的情况。
场景:在静态调用静态类时,在callback函数中使用,赋给一个普通变量,使用use从父域总集成以及传递参数时。
使用:
$greet
=
function
(
$name
)
{
printf(
"Hello %s\r\n"
,
$name
);
};
$greet
(
'World'
);
使用use从父域中继承
$message = 'hello'; // 继承 $message $example = function () use ($message) { var_dump($message); }; echo $example(); // Inherit by-reference $example = function () use (&$message) { var_dump($message); }; echo $example(); // The changed value in the parent scope // is reflected inside the function call $message = 'world';