라이브러리

[PHP] ReflectionFunctionAbstract::getClosureThis - 클로저 내부의 $this에 해당하는 객체를 반환합니다.




ReflectionFunctionAbstract::getClosureThis


PHP의 ReflectionFunctionAbstract 클래스는 PHP의 함수에 대한 정보를 제공하는 클래스입니다. 이 클래스의 getClosureThis 메소드는 클로저의 this 변수를 반환합니다.

클로저의 this 변수는 클로저가 호출될 때 this 키워드와 같은 역할을 합니다. 클로저는 함수 내부에서 정의된 변수를 사용할 수 있지만, 클로저가 호출될 때 this 키워드가 없으면 클로저는 자신의 스코프에서 변수를 찾습니다.

예제


#hostingforum.kr
php

class MyClass {

    public $name;



    public function __construct($name) {

        $this->name = $name;

    }



    public function sayHello() {

        $closure = function() {

            echo "Hello, my name is " . $this->name . "!
";

        };

        $reflection = new ReflectionFunction($closure);

        echo "클로저의 this 변수: " . $reflection->getClosureThis()->name . "
";

    }

}



$obj = new MyClass("John");

$obj->sayHello();



이 예제에서, MyClass의 sayHello 메소드 내부에서 클로저가 정의됩니다. 클로저는 MyClass의 this 변수를 사용합니다. ReflectionFunctionAbstract::getClosureThis 메소드는 클로저의 this 변수를 반환합니다. 이 예제에서는 클로저의 this 변수가 MyClass의 인스턴스인 obj를 참조합니다.

결과


#hostingforum.kr


Hello, my name is John!

클로저의 this 변수: John



참고


* ReflectionFunctionAbstract 클래스는 PHP 5.3 이상에서 사용할 수 있습니다.
* 클로저의 this 변수는 클로저가 호출될 때 this 키워드와 같은 역할을 합니다.
* ReflectionFunctionAbstract::getClosureThis 메소드는 클로저의 this 변수를 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 283 페이지

검색

게시물 검색