개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.03.03 13:39

ReflectionMethod::getClosure 사용시 오류 발생

  • 웹앱구루 12일 전 2025.03.03 13:39
  • 21
    1
저는 ReflectionMethod::getClosure를 사용하여 클래스 메서드에 접근하고자 하는데, 아래와 같은 오류가 발생합니다.

php

$reflectionMethod = new ReflectionMethod('AppHttpControllersExampleController', 'exampleMethod');

$closure = $reflectionMethod->getClosure($this);



위 코드를 실행하면, "Closure is not invocable" 오류가 발생합니다.

getClosure() 메서드의 사용법을 알려주시면 감사하겠습니다.

또한, 이 오류는 어떤 경우에 발생할 수 있는지 알려주시면 도움이 될 것입니다.

감사합니다.

    댓글목록

    profile_image
    나우호스팅  12일 전



    ReflectionMethod::getClosure() 메서드는 클래스 메서드를 호출 가능한 클로저로 반환합니다. 그러나 이 메서드를 사용하여 반환된 클로저를 호출할 때 \"Closure is not invocable\" 오류가 발생하는 경우가 있습니다.

    이 오류는 두 가지 경우에 발생할 수 있습니다.

    1. 클래스 메서드에 인자를 전달할 수 없는 경우: ReflectionMethod::getClosure() 메서드는 클래스 메서드에 인자를 전달할 수 없습니다. 따라서, 클래스 메서드에 인자를 전달할 수 없는 경우 \"Closure is not invocable\" 오류가 발생합니다.

    2. 클래스 메서드가 private 또는 protected 인 경우: ReflectionMethod::getClosure() 메서드는 private 또는 protected 클래스 메서드를 호출할 수 없습니다. 따라서, private 또는 protected 클래스 메서드를 호출할 때 \"Closure is not invocable\" 오류가 발생합니다.

    클래스 메서드를 호출 가능한 클로저로 반환하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflectionMethod = new ReflectionMethod('AppHttpControllersExampleController', 'exampleMethod');
    
    $closure = $reflectionMethod->getClosure(null);
    
    


    위 코드에서 null을 전달하여 클래스 메서드에 인자를 전달하지 않습니다. 또한, private 또는 protected 클래스 메서드를 호출할 때는 ReflectionClass::getClosure() 메서드를 사용하여 클래스를 생성한 후, 클래스의 메서드를 호출할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('AppHttpControllersExampleController');
    
    $reflectionMethod = $reflectionClass->getMethod('exampleMethod');
    
    $closure = $reflectionMethod->getClosure($reflectionClass->newInstance());
    
    


    위 코드에서 ReflectionClass::newInstance() 메서드를 사용하여 클래스를 생성한 후, 클래스의 메서드를 호출할 수 있습니다.

    2025-03-03 13:40

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 3,691건 / 231 페이지

검색

게시물 검색