php中怎麼理解Closure的bind和bindTo

bind是bindTo的靜態版本,所以只說bind吧。(還不是太瞭解爲何要弄出兩個版本)php

官方文檔: 複製一個閉包,綁定指定的$this對象和類做用域。閉包

其實後半句表述很不清楚。 個人理解: 把一個閉包轉換爲某個類的方法(只是這個方法不須要經過對象調用), 這樣閉包中的$this、static、self就轉換成了對應的對象或類。this

 

由於有幾種狀況:對象

一、只綁定$this對象.
二、只綁定類做用域.
三、同時綁定$this對象和類做用域.(文檔的說法)
四、都不綁定.(這樣一來只是純粹的複製, 文檔說法是使用cloning代替bind或bindTo)blog

 

下面詳細講解這幾種狀況:作用域

一、只綁定$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();

  

My name is php, I’m 100 years old.

注意: 在上面的這個例子中,是不能夠在$closure中使用static的,若是須要使用static,經過第三個參數傳入帶命名空間的類名。io

 

二、只綁定類做用域.function

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

  

My name is php, I’m 100 years old.

注意: 在上面的例子中,是不能夠在$closure中使用$this的,由於咱們的bind只綁定了類名,也就是static,若是須要使用$this,新建一個對象做爲bind的第二個參數傳入。class

 

三、同時綁定$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();

  

My name is php, I’m 100 years old. Sex: female.

在這個例子中能夠在$closure中同時使用$this和static

 

四、都不綁定.(這樣一來只是純粹的複製, 文檔說法是使用cloning代替bind或bindTo)

$closure = function () {
    echo "bind nothing.\n";
};

//與$bound_closure = clone $closure;的效果同樣
$bound_closure = Closure::bind($closure, null);

$bound_closure();

  

bind nothing.

這個就用clone好了吧…

相關文章
相關標籤/搜索