소개
익명 함수(람다라고도 함)는 클로저의 객체를 반환합니다. 수업. 이 클래스에는 익명 함수에 대한 추가 제어를 제공하는 몇 가지 추가 메서드가 있습니다.
구문
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
} 방법
비공개 클로저::__construct ( void ) — 이 메소드는 Closure 클래스의 인스턴스화를 허용하지 않기 위해서만 존재합니다. 이 클래스의 객체는 익명 함수에 의해 생성됩니다.
public static Closure::bind ( 클로저 $closure , 객체 $newthis [, mixed $newscope ="static" ] ) - 클로저 — 특정 바인딩된 객체 및 클래스 범위로 클로저를 복제합니다. 이 메서드는 Closure::bindTo()의 정적 버전입니다.
public Closure::bindTo ( object $newthis [, 혼합 $newscope ="static" ] ) - 폐쇄 — 새로운 바인딩된 객체와 클래스 범위로 클로저를 복제합니다. 바디와 바운드 변수가 같지만 객체와 클래스 범위가 다른 새로운 익명 함수를 만들고 반환합니다.
public Closure::call ( object $newthis [, 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")). "\n";;
// using bind method
$sayhello = $hello->bindTo(new A("Amar"),'A');
echo $sayhello();
?> 출력
위의 프로그램은 다음과 같은 출력을 보여줍니다.
Hello Amar Hello Amar