提出

在匿名函数出现之前,所有的函数都需要先命名才能使用

 function increment($value)
{
return $value + 1;
} array_map('increment', [1, 2, 3]);
有的时候函数可能只需要使用一次,这时候使用匿名函数会使得代码更加简洁直观,同时也避免了函数在其他地方被使用
 array_map(function($value){
return $value + 1;
}, [1, 2, 3]);
 
 

定义和使用

PHP 将闭包和匿名函数视为同等概念(本文统称为匿名函数),本质上都是伪装成函数的对象。

匿名函数的本质是对象,因此跟对象一样可将匿名函数赋值给某一变量

 $greet = function(string $name){
echo "hello {$name}";
} $greet("jack") // hello jack
 

所有的匿名函数都是 Closure 对象的实例

$greet instanceof Closure // true
 

对象并没有什么父作用域可言,所以需要使用 use 来手动声明使用的变量,

 $num = 1;
$func = function() use($num){
$num = $num + 1;
echo $num;
}
$func(); //
echo $num; // 还是 1
 

如果要让匿名函数中的变量生效,需要使用引用传值

 $num = 1;
$func = function() use(&$num){
$num = $num + 1;
echo $num;
}
$func(); //
echo $num; //
 

从 PHP 5.4 开始,在类里面使用匿名函数时,匿名函数的 $this 将自动绑定到当前类

 class Foo {
public function bar()
{
return function() {
return $this;
};
}
} $foo = new Foo();
$obj = $foo->bar(); // Closure()
$obj(); // Foo
 

如果不想让自动绑定生效,可使用静态匿名函数

 class Foo {
public function bar()
{
return static function() {
return $this;
};
}
}
$foo = new Foo();
$obj = $foo->bar(); // Closure()
$obj(); // Using $this when not in object context
 
 

匿名函数的本质

匿名函数的本质是 Closure 对象,包括了以下五个方法

 Closure {
private __construct ( void )
public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure
public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure
public call ( object $newthis [, mixed $... ] ) : mixed
public static fromCallable ( callable $callable ) : Closure

}

__construct - 防止匿名函数被实例化

$closure = new \Closure();
// PHP Error: Instantiation of 'Closure' is not allowed
 

Closure::bindTo - 复制当前匿名函数对象,绑定指定的 $this 对象和类作用域。通俗的说,就是手动将匿名函数与指定对象绑定,利用这点,可以扩展对象的功能。

 // 定义商品类
class Good {
private $price; public function __construct(float $price)
{
$this->price = $price;
}
} // 定义一个匿名函数,计算商品的促销价
$addDiscount = function(float $discount = 0.8){
return $this->price * $discount;
} $good = new Good(100); // 将匿名函数绑定到 $good 实例,同时指定作用域为 Good
$count = $addDiscount->bindTo($good, Good::class);
$count(); // 80 // 将匿名函数绑定到 $good 实例,但是不指定作用域,将无法访问 $good 的私有属性
$count = $addDiscount->bindTo($good);
$count(); // 报错
 

Closure::bind - bindTo 方法的静态版本,有两种用法:

用法一:实现与 bindTo 方法同样的效果

$count = \Closure::bind($addDiscount, $good, Good::class); 
 

用法二:将匿名函数与类(而不是对象)绑定,记得要将第二个参数设置为 null

 // 商品库存为 10
class Good {
static $num = 10;
} // 每次销售后返回当前库存
$sell = static function() {
return"当前库存为". --static::$num ;
}; // 将静态匿名函数绑定到 Good 类中
$sold = \Closure::bind($sell, null, Good::class); $sold(); // 当前库存为 9
$sold(); // 当前库存为 8
 

call - PHP 7 新增的 call 方法可以实现绑定并调用匿名函数,除了语法更加简洁外,性能也更高

 // call 版本
$addDiscount->call($good, 0.5); // 绑定并传入参数 0.5,结果为 50 // bindTo 版本
$count = $addDiscount->bindTo($good, Good::class);
$count(0.5); //
 

fromCallable - 将给定的 callable 函数转化成匿名函数

 class Good {
private $price; public function __construct(float $price)
{
$this->price = $price;
}
} function addDiscount(float $discount = 0.8){
return $this->price * $discount;
} $closure = \Closure::fromCallable('addDiscount');
$good = new Good(100);
$count = $closure->bindTo($good);
$count = $closure->bindTo($good, Good::class); // 报错,不能重复绑定作用域
$count(); // 报错,无法访问私有属性
 

fromCallable 等价于

 $reflexion = new ReflectionFunction('addDiscount');
$closure = $reflexion->getClosure();
 

这里有一点需要特别注意的是,无论是 fromCallable 转化成的闭包,还是使用反射得到的闭包,在使用 bindTo时,如果第二个参数指定绑定类,会报错

Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()
 

PHP 核心特性 - 匿名函数的更多相关文章

  1. 匿名函数托管 func-spring-boot-starter

    匿名函数托管 func-spring-boot-starter 项目地址 func-spring-boot-starter开源项目地址: https://gitee.com/yiur/func-spr ...

  2. func-spring-boot-starter 匿名函数托管

    func-spring-boot-starter 匿名函数托管 GitHub项目路径: https://github.com/yiurhub/func-spring-boot-starter Gite ...

  3. PHP 核心特性之匿名函数

    提出 在匿名函数出现之前,所有的函数都需要先命名才能使用 1 2 3 4 5 function increment($value) {     return $value + 1; } array_m ...

  4. 从匿名函数(闭包特性)到 PHP 设计模式之容器模式

    匿名函数(匿名函数) 匿名函数,也叫闭包函数,它允许临时创建一个没有指定名称的函数,常用作回调函数参数的值,也可以作为变量的值来使用.具体的使用见以下示例代码: /* 示例一:声明一个简单匿名函数,并 ...

  5. day03 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  6. C++11新特性:Lambda函数(匿名函数)

    声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 ...

  7. C++11 带来的新特性 (4)—— 匿名函数(Lambdas)

    1 语法 Lambdas并不是新概念,在其它语言中已经烂大街了.直接进入主题,先看语法: [ captures ] ( params ) specifiers exception attr -> ...

  8. Javascript闭包和C#匿名函数对比分析

    C#中引入匿名函数,多少都是受到Javascript的闭包语法和面向函数编程语言的影响.人们发现,在表达式中直接编写函数代码是一种普遍存在的需求,这种语法将比那种必须在某个特定地方定义函数的方式灵活和 ...

  9. ES6核心特性

    摘要:聊JS离不开ES6啊! 原文:ES6核心特性 作者:ljianshu 前言 ES6 虽提供了许多新特性,但我们实际工作中用到频率较高并不多,根据二八法则,我们应该用百分之八十的精力和时间,好好专 ...

随机推荐

  1. 使用AddLayer方法加载shp文件中使用的Map、Dataset等对象详解

    内容源自:ArcGIS Engine+C#入门经典 方法二:使用axMapControl1对象的AddLayer方法加载ShapeFile文件 添加ShapeFile文件需要用到Map.Dataset ...

  2. PHP compact

    1.函数的作用:将变量转成数组 2.函数的参数: @params string $varname1 @params string $varname2 ... @params array $varnam ...

  3. MVC路径无匹配或请求api版本过低时处理

    解决方案:RequestMappingHandlerMapping中重写handleNoMatch方法,springMVC和springboot中配置无区别. 另: 1.可搭配advice处理抛出的异 ...

  4. tkinter基础-标签、按钮

    本节内容: 明白标签.按钮的使用 实现简单的点击界面 Tkinter 简称tk,在python中属于内置模块,不需要进行安装,可直接引用,import tkinter 一. 首先我们做一个如图所示的图 ...

  5. collectionView reloadData走了不执行cellForItemAtIndexPath

    有可能是sizeForItemAtIndexPath方法中的前几个cell没有设置大小, 这里必须设置, 哪怕是设置一个很小的值

  6. 个人记账app(一)需求设计

    时间如流水,只能流去不流回. 学历代表你的过去,能力代表你的现在,学习能力代表你的将来. 学无止境,精益求精. 一.开发背景 Android应用市场记账的app那么多,我为什么还要再开发一个呢?重复造 ...

  7. 模块基础 day15

    目录 模块的四种形式 内置模块 pip安装的模块 自定义模块 包(模块) import和from···import 循环导入 模块的搜索路径 python文件的两种用途 模块的四种形式 模块就是一系列 ...

  8. Andriod一段时间未操作页面,系统自动登出

    功能描述: APP在公共的设备上运行,出于安全考虑,当登录的用户在超过一定时间内没有做任何操作, 则系统自动登出,用户如需重新操作APP,则需要重新登录 . 第一步:创建一个BaseActivity ...

  9. 向现有URL末尾添加查询字符串参数

    向现有URL末尾添加查询字符串参数 xhr.open("get", "example.php?name1=value1&name2=value2", t ...

  10. [UWP]使用GetAlphaMask和ContainerVisual制作长阴影(Long Shadow)

    1. 什么是长阴影 前几年扁平化设计(Flat Design)十分流行,后来在扁平化的基础上又流行起了长阴影(Long Shadow).长阴影其实就是扩展了对象的投影,感觉是一种光线照射下的影子,通常 ...