개발자 Q&A

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

2025.06.29 04:44

ReflectionFunctionAbstract::isGenerator에 관한 질문

목록
  • NoSQL구루 1일 전 2025.06.29 04:44
  • 6
    1
저는 ReflectionFunctionAbstract::isGenerator를 사용하여 함수의 제너레이터 여부를 확인하려고 하는데요.
제너레이터 함수의 경우 true를 반환하고 일반 함수의 경우 false를 반환하는 것 같은데요.
하지만, 내부에서 사용하는 함수가 제너레이터 함수일 때는 false를 반환하는 것 같아요.
이러한 경우를 대비하여 ReflectionFunctionAbstract::isGenerator가 제너레이터 함수를 호출하는지 여부를 확인할 수 있는 방법이 있나요?

    댓글목록

    profile_image
    나우호스팅  1일 전



    ReflectionFunctionAbstract::isGenerator는 함수의 제너레이터 여부를 확인하는 메서드입니다. 하지만, 내부에서 사용하는 함수가 제너레이터 함수일 때는 false를 반환하는 경우가 있습니다.

    이러한 경우를 대비하여 ReflectionFunctionAbstract::isGenerator가 제너레이터 함수를 호출하는지 여부를 확인할 수 있는 방법은 다음과 같습니다.

    1. ReflectionFunctionAbstract::getClosure() 메서드를 사용하여 함수의 클로저를 가져옵니다.
    2. 클로저가 함수를 호출하는지 여부를 확인합니다. 클로저가 함수를 호출하는 경우, ReflectionFunctionAbstract::isGenerator가 제너레이터 함수를 호출하는 것으로 간주할 수 있습니다.

    다음은 예제 코드입니다.

    #hostingforum.kr
    php
    
    function testGenerator() {
    
        yield 1;
    
    }
    
    
    
    function testFunction() {
    
        return 1;
    
    }
    
    
    
    $reflectionFunction1 = new ReflectionFunction('testGenerator');
    
    $reflectionFunction2 = new ReflectionFunction('testFunction');
    
    
    
    echo $reflectionFunction1->isGenerator ? 'true' : 'false'; // true
    
    echo "n";
    
    echo $reflectionFunction2->isGenerator ? 'true' : 'false'; // false
    
    
    
    $closure1 = $reflectionFunction1->getClosure();
    
    $closure2 = $reflectionFunction2->getClosure();
    
    
    
    echo $closure1->getFileName() . ':' . $closure1->getStartLine() . ' ' . $closure1->getDocComment() . "n";
    
    echo $closure2->getFileName() . ':' . $closure2->getStartLine() . ' ' . $closure2->getDocComment() . "n";
    
    
    
    echo $closure1->hasStaticReference() ? 'true' : 'false'; // true
    
    echo "n";
    
    echo $closure2->hasStaticReference() ? 'true' : 'false'; // false
    
    


    위 예제 코드에서, `testGenerator` 함수는 제너레이터 함수이므로 ReflectionFunctionAbstract::isGenerator가 true를 반환합니다. 하지만, `testFunction` 함수는 일반 함수이므로 ReflectionFunctionAbstract::isGenerator가 false를 반환합니다.

    `testGenerator` 함수의 클로저는 함수를 호출하는 것으로 간주할 수 있습니다. 따라서, ReflectionFunctionAbstract::isGenerator가 제너레이터 함수를 호출하는 것으로 간주할 수 있습니다. 반면, `testFunction` 함수의 클로저는 함수를 호출하지 않는 것으로 간주할 수 있습니다. 따라서, ReflectionFunctionAbstract::isGenerator가 제너레이터 함수를 호출하는 것으로 간주할 수 없습니다.

    2025-06-29 04:45

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

검색

게시물 검색