PHP 的Closure的bind 详细介绍

本文详细介绍了PHP中闭包的四种绑定方式:仅绑定$this对象、仅绑定类作用域、同时绑定$this对象和类作用域及完全不绑定的情况。通过具体示例展示了不同绑定方式下闭包的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:https://www.cnblogs.com/eleven24/p/7487923.html


1、只绑定$this对象.
2、只绑定类作用域.
3、同时绑定$this对象和类作用域.(文档的说法)

4、都不绑定.(这样一来只是纯粹的复制, 文档说法是使用cloning代替bind或bindTo)

第一种 只绑定$this对象

$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();

第二种 只绑定类作用域.

$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();

第三种 同时绑定$this对象和类作用域.(文档的说法)

$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();


4、都不绑定.(这样一来只是纯粹的复制, 文档说法是使用cloning代替bind或bindTo)

$closure function () {
    echo "bind nothing.\n";
};
 
//与$bound_closure = clone $closure;的效果一样
$bound_closure = Closure::bind($closure, null);
 
$bound_closure();



[10501] PDOException in Connection.php line 687 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'c.mobile' in 'field list' $this->debug(false, '', $master); // 返回结果集 return $this->getResult($pdo, $procedure); } catch (\PDOException $e) { if ($this->isBreak($e)) { return $this->close()->query($sql, $bind, $master, $pdo); } throw new PDOException($e, $this->config, $this->getLastsql()); } catch (\Throwable $e) { if ($this->isBreak($e)) { return $this->close()->query($sql, $bind, $master, $pdo); } throw $e; } catch (\Exception $e) { if ($this->isBreak($e)) { return $this->close()->query($sql, $bind, $master, $pdo); Call Stack in Connection.php line 687 at Connection->query('SELECT `c`.`id`,`c`....', [], false, false) in Connection.php line 923 at Connection->select(object(Query)) in Query.php line 3007 at Query->select() in Index.php line 36 at Index->index() at ReflectionMethod->invokeArgs(object(Index), []) in Container.php line 395 at Container->invokeReflectMethod(object(Index), object(ReflectionMethod), []) in Module.php line 132 at Module->think\route\dispatch\{closure}(object(Request), object(Closure), null) at call_user_func_array(object(Closure), [object(Request), object(Closure), null]) in Middleware.php line 185 at Middleware->think\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Middleware.php line 130 at Middleware->dispatch(object(Request), 'controller') in Module.php line 137 at Module->exec() in Dispatch.php line 168 at Dispatch->run() in App.php line 432 at App->think\{closure}(object(Request), object(Closure), null) at call_user_func_array(object(Closure), [object(Request), object(Closure), null]) in Middleware.php line 185 at Middleware->think\{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Middleware.php line 130 at Middleware->dispatch(object(Request)) in App.php line 435 at App->run() in index.php line 21
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值