PHP 闭包的理解
在官方文档定义中,匿名函数(Anonymous functions),也叫闭包函数(closures),允许临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。
在laravel应用中,也是使用非常频繁的语法特性
例如在定义路由时,通过闭包的方法,定义路由组
Route::middleware(['first', 'second'])->group(function () {
Route::get('/', function () {
// // 使用 first 和 second 中间件
});
Route::get('user/profile', function () {
// // 使用 first 和 second 中间件
});
});
laravel的许多集合函数也通过闭包来实现,例如
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [3, 4]
以下示例来自官方文档匿名函数
匿名函数目前是通过 Closure 类来实现的。
<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// 输出 helloWorld
?>
闭包函数也可以作为变量的值来使用。PHP 会自动把此种表达式转换成内置类 Closure 的对象实例。把一个 closure 对象赋值给一个变量的方式与普通变量赋值的语法是一样的,最后也要加上分号:
<?php
$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World');//输出Hello World
$greet('PHP');//输出Hello PHP
?>
闭包可以从父作用域中继承变量。 任何此类变量都应该用 use 语言结构传递进去。 PHP 7.1 起,不能传入此类变量: superglobals、 $this 或者和参数重名。
<?php
$message = 'hello';
// 没有 "use"
$example = function () {
var_dump($message);
};
echo $example();//Notice: Undefined variable: message in /example.php on line 6
//输出NULL
// 继承 $message
$example = function () use ($message) {
var_dump($message);
};
echo $example();
//输出hello
// Inherited variable's value is from when the function
// is defined, not when called
$message = 'world';
echo $example();
//输出hello
// Reset message
$message = 'hello';
// Inherit by-reference
$example = function () use (&$message) {
var_dump($message);
};
echo $example();
//输出hello
// The changed value in the parent scope
// is reflected inside the function call
$message = 'world';
echo $example();
//输出world
// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
var_dump($arg . ' ' . $message);
};
$example("hello");
//输出hello world
?>
Closure::bind
官方文档给出定义如下:
Closure::bind — 复制一个闭包,绑定指定的$this对象和类作用域。
我的理解如下:
可通过预定义一个闭包,在后续中,通过bind方法传入类对象和作用域,使闭包成为类的方法,laravel的宏指令就有使用到此方法,后续会做相应研究。
根据官方给出的案例
<?php
class A {
private static $sfoo = 1;
private $ifoo = 2;
}
$cl1 = static function() {
return A::$sfoo;
};
$cl2 = function() {
return $this->ifoo;
};
$bcl1 = Closure::bind($cl1, null, 'A');
$bcl2 = Closure::bind($cl2, new A(), 'A');
echo $bcl1(), "\n";//输出1
echo $bcl2(), "\n";//输出2
?>
Closure::bind( Closure $closure , object $newthis [, mixed $newscope = ‘static’)可传入的参数三个参数中,其中$closure为匿名函数,主要讨论后面两个参数$newthis,$newscope为绑定指定的$this对象和类作用域。
在以上案例中,bind方法中$bcl1只传入了类作用域,代表在$cl1闭包函数作用域中允许访问类的静态变量,但不能访问类的变量和类的方法。
$bcl2传入了类作用域和$this对象,代表在$cl2可以访问类的所有变量和类的所有方法,此时可通过闭包完成对类方法的扩展。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: