개발자 Q&A

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

2025.03.27 02:48

ReflectionParameter::isPromoted에 대한 이해

목록
  • Dart광 4일 전 2025.03.27 02:48
  • 6
    1
선생님, ReflectionParameter::isPromoted 메소드에 대해 공부중인데, 이 메소드는 어떤 상황에서 true를 반환하는지 잘 이해가 가지 않습니다.

예를 들어, 아래 코드에서 ReflectionParameter::isPromoted 메소드가 어떻게 동작하는지 알려주실 수 있을까요?

php

use ReflectionClass;

use ReflectionParameter;



class Test {

    public function testMethod(ReflectionParameter $param) {

        var_dump($param->isPromoted());

    }

}



$reflectionClass = new ReflectionClass('Test');

$reflectionMethod = $reflectionClass->getMethod('testMethod');

$reflectionParameter = $reflectionMethod->getParameters()[0];



$test = new Test();

$test->testMethod($reflectionParameter);



이 코드에서 ReflectionParameter::isPromoted 메소드가 어떻게 동작하는지 알려주실 수 있을까요?

    댓글목록

    profile_image
    나우호스팅  4일 전



    ReflectionParameter::isPromoted 메소드는 PHP 7.0 이상에서 사용할 수 있는 메소드입니다. 이 메소드는 인자로 전달된 ReflectionParameter 객체가 PHP 7.0 이상에서 사용하는 타입 힌트에 따라 promoted(승격)된 인자인지 여부를 반환합니다.

    예를 들어, 아래 코드에서 ReflectionParameter::isPromoted 메소드는 어떻게 동작하는지 살펴보겠습니다.

    #hostingforum.kr
    php
    
    use ReflectionClass;
    
    use ReflectionParameter;
    
    
    
    class Test {
    
        public function testMethod(ReflectionParameter $param) {
    
            var_dump($param->isPromoted());
    
        }
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('Test');
    
    $reflectionMethod = $reflectionClass->getMethod('testMethod');
    
    $reflectionParameter = $reflectionMethod->getParameters()[0];
    
    
    
    $test = new Test();
    
    $test->testMethod($reflectionParameter);
    
    


    위 코드에서, $reflectionParameter 객체는 타입 힌트가 ReflectionParameter 인 경우에만 승격된 인자로 간주됩니다. 따라서, 위 코드에서 ReflectionParameter::isPromoted 메소드는 false를 반환합니다.

    하지만, 아래 코드에서 타입 힌트가 ReflectionParameter 인 경우에만 승격된 인자로 간주됩니다.

    #hostingforum.kr
    php
    
    use ReflectionClass;
    
    use ReflectionParameter;
    
    
    
    class Test {
    
        public function testMethod(ReflectionParameter $param) {
    
            var_dump($param->isPromoted());
    
        }
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('Test');
    
    $reflectionMethod = $reflectionClass->getMethod('testMethod');
    
    $reflectionParameter = $reflectionMethod->getParameters()[0];
    
    
    
    $test = new Test();
    
    $reflectionParameter->setType(ReflectionParameter::TYPE_OBJECT);
    
    $test->testMethod($reflectionParameter);
    
    


    위 코드에서, ReflectionParameter::isPromoted 메소드는 true를 반환합니다.

    따라서, ReflectionParameter::isPromoted 메소드는 PHP 7.0 이상에서 사용하는 타입 힌트에 따라 승격된 인자인지 여부를 반환합니다.

    2025-03-27 02:49

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

검색

게시물 검색