php Closure::bind的用法(转)
官方文档:Closure 类
原文:php中怎么理解Closure的bind和bindTo
bind是bindTo的静态版本,因此只说bind吧。(还不是太了解为什么要弄出两个版本)
官方文档: 复制一个闭包,绑定指定的$this对象和类作用域。
其实后半句表述很不清楚。 我的理解: 把一个闭包转换为某个类的方法(只是这个方法不需要通过对象调用), 这样闭包中的$this、static、self就转换成了对应的对象或类。
因为有几种情况:
1、只绑定$this对象.
2、只绑定类作用域.
3、同时绑定$this对象和类作用域.(文档的说法)
4、都不绑定.(这样一来只是纯粹的复制, 文档说法是使用cloning代替bind或bindTo)
下面详细讲解这几种情况:
1、只绑定$this对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$closure = function ( $name , $age ) { $this ->name = $name ; $this ->age = $age ; }; class Person { public $name ; public $age ; public function say() { echo "My name is {$this->name}, I'm {$this->age} years old.\n" ; } } $person = new Person(); //把$closure中的$this绑定为$person //这样在$bound_closure中设置name和age的时候实际上是设置$person的name和age //也就是绑定了指定的$this对象($person) $bound_closure = Closure::bind( $closure , $person ); $bound_closure ( 'php' , 100); $person ->say(); |
1
|
My name is php, I’m 100 years old. |
注意: 在上面的这个例子中,是不可以在$closure中使用static的,如果需要使用static,通过第三个参数传入带命名空间的类名。
2、只绑定类作用域.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$closure = function ( $name , $age ) { static :: $name = $name ; static :: $age = $age ; }; class Person { static $name ; static $age ; public static function say() { echo "My name is " . static :: $name . ", I'm " . static :: $age . " years old.\n" ; } } //把$closure中的static绑定为Person类 //这样在$bound_closure中设置name和age的时候实际上是设置Person的name和age //也就是绑定了指定的static(Person) $bound_closure = Closure::bind( $closure , null, Person:: class ); $bound_closure ( 'php' , 100); Person::say(); |
1
|
My name is php, I’m 100 years old. |
注意: 在上面的例子中,是不可以在$closure中使用$this的,因为我们的bind只绑定了类名,也就是static,如果需要使用$this,新建一个对象作为bind的第二个参数传入。
3、同时绑定$this对象和类作用域.(文档的说法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
$closure = function ( $name , $age , $sex ) { $this ->name = $name ; $this ->age = $age ; static :: $sex = $sex ; }; class Person { public $name ; public $age ; static $sex ; public function say() { echo "My name is {$this->name}, I'm {$this->age} years old.\n" ; echo "Sex: " . static :: $sex . ".\n" ; } } $person = new Person(); //把$closure中的static绑定为Person类, $this绑定为$person对象 $bound_closure = Closure::bind( $closure , $person , Person:: class ); $bound_closure ( 'php' , 100, 'female' ); $person ->say(); |
1
|
My name is php, I’m 100 years old. Sex: female. |
在这个例子中可以在$closure中同时使用$this和static
4、都不绑定.(这样一来只是纯粹的复制, 文档说法是使用cloning代替bind或bindTo)
1
2
3
4
5
6
7
8
|
$closure = function () { echo "bind nothing.\n" ; }; //与$bound_closure = clone $closure;的效果一样 $bound_closure = Closure::bind( $closure , null); $bound_closure (); |
1
|
bind nothing. |
这个就用clone好了吧…
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)