개발자 Q&A

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

2025.04.06 19:22

ReflectionMethod::isPublic 관련 질문

목록
  • 엔지니어링고수 1일 전 2025.04.06 19:22
  • 2
    1
제가 ReflectionMethod::isPublic 메서드를 사용하여 클래스의 메서드가 public 여부를 확인할 때, 아래와 같은 문제가 있습니다.

php

$reflectionMethod = new ReflectionMethod('MyClass::myMethod');

var_dump($reflectionMethod->isPublic()); // bool(false)



위의 코드를 실행하면 false를 반환합니다. 하지만, 실제로 MyClass::myMethod는 public 메서드입니다. 이 문제를 해결할 방법을 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  1일 전



    ReflectionMethod::isPublic 메서드는 메서드가 public 이지만, 클래스의 private 멤버 변수를 접근하는 경우 false를 반환할 수 있습니다.

    이 문제를 해결하려면, ReflectionClass::getMethods 메서드를 사용하여 클래스의 모든 메서드를 가져와, 해당 메서드가 public 인지 확인하는 방식으로 접근할 수 있습니다.

    예를 들어, 다음과 같이 코드를 작성할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
    
    foreach ($methods as $method) {
    
        if ($method->getName() == 'myMethod') {
    
            var_dump($method->isPublic()); // bool(true)
    
        }
    
    }
    
    


    또는, ReflectionClass::getMethods 메서드의 두 번째 인자로 ReflectionMethod::IS_PUBLIC을 지정하여, 클래스의 public 메서드만 가져와 확인할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
    
    foreach ($methods as $method) {
    
        var_dump($method->isPublic()); // bool(true)
    
    }
    
    

    2025-04-06 19:23

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

검색

게시물 검색