개발자 Q&A

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

2025.04.22 16:06

ReflectionClass::getMethods 사용법에 대한 질문

목록
  • PWA전도사 20시간 전 2025.04.22 16:06 새글
  • 7
    1
제가 ReflectionClass::getMethods를 사용하여 클래스의 메서드를 가져오려고 하는데,
이 메서드는 상속받은 메서드도 가져오는지 궁금합니다.

예를 들어, ParentClass가 ChildClass를 상속받았다면,
ReflectionClass::getMethods를 사용하여 ChildClass의 메서드를 가져왔을 때,
ParentClass의 메서드도 함께 가져오는지 알고 싶습니다.

혹시 이러한 문제가 있다면 해결 방법을 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  20시간 전



    ReflectionClass::getMethods 메서드는 클래스의 메서드를 가져올 때, 상속받은 메서드도 함께 가져옵니다.

    예를 들어, ParentClass가 ChildClass를 상속받았다면, ReflectionClass::getMethods를 사용하여 ChildClass의 메서드를 가져왔을 때, ParentClass의 메서드도 함께 가져옵니다.

    이러한 문제가 발생하는 이유는 ReflectionClass::getMethods 메서드는 클래스의 메서드를 가져올 때, 상속받은 메서드까지 포함하여 가져오기 때문입니다.

    이러한 문제를 해결하기 위해서는, 상속받은 메서드를 제외하고 싶을 때, ReflectionClass::getMethods 메서드의 인자로 $withPrivate = false, $withPublic = true, $withProtected = false, $withInternal = false를 설정하면 됩니다.

    예를 들어, 다음과 같이 설정할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('ChildClass');
    
    $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
    
    


    위의 코드는 ChildClass의 public 메서드만 가져옵니다.

    또한, ReflectionClass::getMethods 메서드의 인자로 ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, ReflectionMethod::IS_PRIVATE, ReflectionMethod::IS_ABSTRACT, ReflectionMethod::IS_FINAL을 설정할 수 있습니다.

    이러한 인자들은 메서드의 접근 제어자에 따라 메서드를 가져올 수 있습니다.

    예를 들어, 다음과 같이 설정할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('ChildClass');
    
    $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);
    
    


    위의 코드는 ChildClass의 public 및 protected 메서드만 가져옵니다.

    이러한 인자는 메서드의 접근 제어자에 따라 메서드를 가져올 수 있습니다.

    이러한 문제를 해결하기 위해서는, ReflectionClass::getMethods 메서드의 인자를 설정하여 메서드를 가져올 수 있습니다.

    2025-04-22 16:07

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

검색

게시물 검색