匿名函数,也叫闭包函数(closures) ,允许临时创建一个没有制定名称的函数。最常用作回调函数(callback)参数的值。

闭包函数也可以作为变量的值来使用。php将会自动把此种表达式转换成内置类 closure 的对象实例。把一个 closure 对象赋值给一个变量的方式与普通变量赋值的语法一样,最后也要加上分号。

匿名函数变量赋值实例:
<?php
$printstring = function($arg){
echo $arg;
};

$printstring(‘hello world’);
//输出 hello world

闭包函数继承父作用域中的变量。任何此类变量都应该用 use 语言结构传递进去。

从父作用域继承变量:
<?php
//定义变量
$message = ‘hello world’;
//匿名函数变量赋值
$example = function(){
var_dump($message);
};
//执行后输出 notice: undefined variable
$example();

在未使用关键字use 时,php不能在匿名函数中调用所在代码上下文变量。
<?php
//定义变量
$message = ‘hello’;
//匿名函数继承父作用域的变量($message)
$example = function() use ($message){
var_dump($message);
};
//输出 string ‘hello’ (length=5)
echo $example();
//同样输出 string ‘hello’ (length=5)
$message = ‘world’;
echo $example();

使用关键字use时,php可以在调用匿名函数中调用所在代码上下文的变量,但为什么第二次调用没有发生变化哪?
是因为匿名函数可以保存所在代码块上下文的一些变量和值(即:闭包函数将会保存第一次继承的父作用域的变量和值),值传递只是传递继承父作用域中变量和值的一个副本。

要想改变父作用域的值并体现在匿名函数调用中,该怎么办哪?
我们要用引用传递(即:在变量前面添加&),如下所示:
<?php
//定义变量
$message = ‘hello’;
//匿名函数继承父作用域的变量($message)
$example = function() use (&$message){
var_dump($message);
};
//输出 string ‘hello’ (length=5)
echo $example();
//输出 string ‘world’ (length=5)
$message = ‘world’;
echo $example();

同样闭包函数也可以接受常规参数的传递,如下所示:

<?php
//定义变量
$message = ‘hello’;
$message2 = ‘hello2’;
//匿名函数继承父作用域的变量($message)
$example = function($arg) use (&$message2, $message){
var_dump($message2 . ‘ ‘ . $message . ‘ ‘ . $arg);
};
echo $example(‘world’);
$message2 = ‘world’;
//输出 string ‘hello world’ (length=11)
echo $example(‘world’);