PHP Closure 類
介紹
匿名函式(也稱為 lambda)返回 Closure 類的物件。此類具有一些額外方法,可進一步控制匿名函式。
語法
Closure { /* Methods */ private __construct ( void ) public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure public call ( object $newthis [, mixed $... ] ) : mixed public static fromCallable ( callable $callable ) : Closure }
方法
private Closure::__construct ( void ) — 此方法僅用於禁止對 Closure 類的例項化。此類物件由匿名函式建立。
public static Closure::bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) − Closure — 複製具有特定繫結物件和類作用域的閉包。此方法是 Closure::bindTo() 的靜態版本。
public Closure::bindTo ( object $newthis [, mixed $newscope = "static" ] ) − Closure — 複製具有新繫結物件和類作用域的閉包。建立並返回一個具有相同主體和繫結變數但具有不同物件和新類範圍的新匿名函式。
public Closure::call ( object $newthis [, mixed $... ] ) − mixed — 暫時將閉包繫結到 newthis,並使用任何給定的引數呼叫它。
閉包示例
<?php class A { public $nm; function __construct($x){ $this->nm=$x; } } // Using call method $hello = function() { return "Hello " . $this->nm; }; echo $hello->call(new A("Amar")). "
";; // using bind method $sayhello = $hello->bindTo(new A("Amar"),'A'); echo $sayhello(); ?>
輸出
以上程式顯示以下輸出
Hello Amar Hello Amar
廣告