
ReflectionMethod::getClosure() 메서드는 private 메서드에 접근할 수 없습니다. 따라서 private 메서드인 getPrivateVar() 메서드의 클로저를 가져오려면 public 메서드를 사용하여 접근해야 합니다.
클로저를 가져오기 위해서는 private 메서드에 접근할 수 있는 public 메서드를 하나 더 생성하여 클로저를 가져오는 메서드를 호출하도록 수정하면 됩니다.
#hostingforum.kr
php
class Test {
private $privateVar;
public function __construct() {
$this->privateVar = 'private value';
}
public function getClosure() {
$closure = ReflectionMethod::getClosure($this, 'getPrivateVar');
return $closure;
}
public function getPrivateVarPublic() {
return $this->getPrivateVar();
}
private function getPrivateVar() {
return $this->privateVar;
}
}
위의 코드에서 getPrivateVarPublic() 메서드는 private 메서드인 getPrivateVar() 메서드를 호출하여 클로저를 가져옵니다.
#hostingforum.kr
php
$closure = new Test();
$closure->getPrivateVarPublic();
이러한 방법으로 private 메서드에 접근할 수 있는 public 메서드를 하나 더 생성하여 클로저를 가져올 수 있습니다.
2025-03-14 15:17