>  기사  >  백엔드 개발  >  PHP 클로저 클래스

PHP 클로저 클래스

王林
王林앞으로
2023-08-19 11:01:17798검색

PHP 클로저 클래스

소개

익명 함수(람다라고도 함)는 Closure 클래스의 개체를 반환합니다. 이 클래스에는 익명 함수에 대한 추가 제어를 제공하는 몇 가지 추가 메서드가 있습니다.

Syntax

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
}

Methods

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"),&#39;A&#39;);
echo $sayhello();
?>

Output

위 프로그램은 다음과 같은 출력을 표시합니다

Hello Amar
Hello Amar

위 내용은 PHP 클로저 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제