개발자 Q&A

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

2025.06.10 21:22

ReflectionClass::getMethods()와 함수의 반환값 이해

목록
  • 화이트해커연구가 2일 전 2025.06.10 21:22
  • 8
    1
제가 ReflectionClass::getMethods() 함수를 사용하여 클래스의 메서드 목록을 가져오려 하지만, 반환값이 array of ReflectionMethod 인 것을 알았습니다. 하지만 array 안에 있는 각 ReflectionMethod 객체를 어떻게 사용해야 하는지 모르겠습니다.

반환된 array의 각 요소는 ReflectionMethod의 인스턴스 인가요? 그리고 그 인스턴스를 사용하여 어떤 메서드를 호출해야 하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  2일 전



    ReflectionClass::getMethods() 함수는 클래스의 메서드 목록을 가져올 때 array of ReflectionMethod 객체를 반환합니다.

    반환된 array의 각 요소는 ReflectionMethod의 인스턴스입니다. 이 인스턴스를 사용하여 메서드를 호출할 수 있습니다.

    예를 들어, 다음 코드는 ReflectionClass::getMethods() 함수를 사용하여 클래스의 메서드 목록을 가져와 각 메서드를 호출하는 방법을 보여줍니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $methods = $reflectionClass->getMethods();
    
    
    
    foreach ($methods as $method) {
    
        $methodName = $method->getName();
    
        $reflectionMethod = new ReflectionMethod($reflectionClass->getName(), $methodName);
    
        $reflectionMethod->invoke($reflectionClass);
    
    }
    
    


    위 코드에서, `$method` 변수는 각 메서드의 ReflectionMethod 인스턴스를 나타냅니다. `$method->getName()` 메서드를 사용하여 메서드 이름을 가져올 수 있습니다. `$method->invoke()` 메서드를 사용하여 메서드를 호출할 수 있습니다. `$method->invoke()` 메서드는 첫 번째 인자로 메서드를 호출할 객체를 받습니다.

    위 코드에서, `$reflectionClass` 객체를 첫 번째 인자로 전달하여 메서드를 호출합니다.

    또한, `$method->getParameters()` 메서드를 사용하여 메서드의 매개변수를 가져올 수 있습니다. `$method->getReturnType()` 메서드를 사용하여 메서드의 반환 타입을 가져올 수 있습니다.

    이러한 메서드를 사용하여 ReflectionClass::getMethods() 함수의 반환값을 더 깊이 이해할 수 있습니다.

    2025-06-10 21:23

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

검색

게시물 검색