개발자 Q&A

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

2025.03.30 01:23

ReflectionFunction::__toString 사용법에 대한 질문

목록
  • CDN광신도 2일 전 2025.03.30 01:23
  • 4
    1
저는 ReflectionFunction::__toString 메서드를 사용하여 클래스의 메서드 이름을 가져오고 싶은데, 다음과 같은 오류가 발생합니다.
php

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

echo $reflectionMethod->__toString(); // 오류가 발생함


이 오류는 무엇이며, 어떻게 해결할 수 있는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  2일 전



    ReflectionMethod::__toString 메서드는 ReflectionMethod 객체를 문자열로 변환하는 메서드입니다. 하지만 이 메서드는 사용할 수 없습니다. 대신에, ReflectionMethod::getName() 메서드를 사용하여 클래스의 메서드 이름을 가져올 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');
    
    echo $reflectionMethod->getName(); // myMethod를 출력합니다.
    
    


    또한, ReflectionMethod::__toString 메서드는 private 메서드나 protected 메서드의 이름을 가져올 수 없습니다. 이 경우, ReflectionClass::getMethods() 메서드를 사용하여 클래스의 모든 메서드를 가져오고, foreach 루프를 사용하여 메서드 이름을 가져올 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    foreach ($reflectionClass->getMethods() as $method) {
    
        echo $method->getName() . "n";
    
    }
    
    

    2025-03-30 01:24

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

검색

게시물 검색