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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP