Closure 類(lèi)

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

簡(jiǎn)介

用于代表 匿名函數(shù) 的類(lèi).

匿名函數(shù)會(huì)產(chǎn)生這種類(lèi)型的對(duì)象。這個(gè)類(lèi)帶有一些方法允許在匿名函數(shù)創(chuàng)建后對(duì)其進(jìn)行更多的控制。

除了此處列出的方法,還有一個(gè) __invoke 方法。這是為了與其他實(shí)現(xiàn)了 __invoke()魔術(shù)方法 的對(duì)象保持一致性,但調(diào)用匿名函數(shù)的過(guò)程與它無(wú)關(guān)。

類(lèi)摘要

final class Closure {
/* 方法 */
private __construct()
public static bind(Closure $closure, ?object $newThis, object|string|null $newScope = "static"): ?Closure
public bindTo(?object $newThis, object|string|null $newScope = "static"): ?Closure
public call(object $newThis, mixed ...$args): mixed
public static fromCallable(callable $callback): Closure
}

目錄

  • Closure::__construct — 用于禁止實(shí)例化的構(gòu)造函數(shù)
  • Closure::bind — 復(fù)制一個(gè)閉包,綁定指定的$this對(duì)象和類(lèi)作用域。
  • Closure::bindTo — 復(fù)制當(dāng)前閉包對(duì)象,綁定指定的$this對(duì)象和類(lèi)作用域。
  • Closure::call — 綁定并調(diào)用閉包
  • Closure::fromCallable — 將 callable 轉(zhuǎn)換為閉包