라이브러리
[PHP] ReflectionGenerator::getThis - 생성기의 $this 값을 가져옵니다.
ReflectionGenerator::getThis
PHP 7.1 버전부터 ReflectionGenerator 클래스가 추가되었으며, 이 클래스는 함수나 메소드의 실행 컨텍스트를 반영하는 데 사용됩니다. ReflectionGenerator::getThis 메소드는 현재 실행 중인 함수나 메소드의 $this 변수를 반환합니다.
사용 예제
#hostingforum.kr
php
class MyClass {
public function myMethod() {
$reflection = new ReflectionFunction('myMethod');
$generator = $reflection->getClosureThis();
echo $generator->getName() . "
"; // MyClass
echo $generator->getClass() . "
"; // MyClass
}
}
$obj = new MyClass();
$obj->myMethod();
설명
위 예제에서, `ReflectionFunction` 클래스를 사용하여 `myMethod` 함수의 반영을 생성합니다. 그런 다음, `getClosureThis` 메소드를 사용하여 현재 실행 중인 함수의 `$this` 변수를 가져옵니다. `$generator` 변수는 현재 실행 중인 함수의 `$this` 변수를 나타내며, `getName` 메소드를 사용하여 함수 이름을 가져올 수 있고, `getClass` 메소드를 사용하여 함수가 속한 클래스를 가져올 수 있습니다.
추가 예제
#hostingforum.kr
php
class MyClass {
public function myMethod() {
$reflection = new ReflectionFunction('myMethod');
$generator = $reflection->getClosureThis();
$generator->myOtherMethod();
}
public function myOtherMethod() {
echo "myOtherMethod 호출됨
";
}
}
$obj = new MyClass();
$obj->myMethod();
설명
위 예제에서, `myMethod` 함수에서 `myOtherMethod` 함수를 호출합니다. `getClosureThis` 메소드를 사용하여 현재 실행 중인 함수의 `$this` 변수를 가져온 후, `myOtherMethod` 함수를 호출합니다. `myOtherMethod` 함수는 `myMethod` 함수의 `$this` 변수를 사용하여 호출되며, `myMethod` 함수의 `$this` 변수를 나타내는 `$generator` 변수를 사용하여 호출됩니다.
결론
ReflectionGenerator::getThis 메소드는 현재 실행 중인 함수나 메소드의 `$this` 변수를 반환하는 데 사용할 수 있습니다. 이 기능을 사용하면 함수나 메소드의 실행 컨텍스트를 반영하고, 함수나 메소드의 `$this` 변수를 가져올 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.