Closure::bind

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

Closure::bind 復(fù)制一個(gè)閉包,綁定指定的$this對(duì)象和類作用域。

說(shuō)明

public static Closure::bind(Closure $closure, ?object $newThis, object|string|null $newScope = "static"): ?Closure

這個(gè)方法是 Closure::bindTo() 的靜態(tài)版本。查看它的文檔獲取更多信息。

參數(shù)

closure

需要綁定的匿名函數(shù)。

newThis

需要綁定到匿名函數(shù)的對(duì)象,或者 null 創(chuàng)建未綁定的閉包。

newScope

想要綁定給閉包的類作用域,或者 'static' 表示不改變。如果傳入一個(gè)對(duì)象,則使用這個(gè)對(duì)象的類型名。 類作用域用來(lái)決定在閉包中 $this 對(duì)象的 私有、保護(hù)方法 的可見性。 不允許內(nèi)置類(的對(duì)象)作為參數(shù)傳遞。

返回值

返回一個(gè)新的 Closure 對(duì)象,失敗時(shí)返回 null。

范例

示例 #1 Closure::bind() 示例

<?php
class {
    private static 
$sfoo 1;
    private 
$ifoo 2;
}
$cl1 = static function() {
    return 
A::$sfoo;
};
$cl2 = function() {
    return 
$this->ifoo;
};

$bcl1 Closure::bind($cl1null'A');
$bcl2 Closure::bind($cl2, new A(), 'A');
echo 
$bcl1(), "\n";
echo 
$bcl2(), "\n";
?>

以上例程的輸出類似于:

1
2

參見