개발자 Q&A

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

2025.06.23 20:23

ReflectionMethod::getPrototype과 Prototype의 차이

목록
  • 제로데이헌터 20시간 전 2025.06.23 20:23 새글
  • 3
    1
제가 ReflectionMethod::getPrototype을 사용하여 Prototype을 가져왔을 때,
Prototype은 ReflectionClass::getPrototype이 가져온 Prototype과 다르게 나타납니다.
어떻게 보면 ReflectionMethod::getPrototype은 ReflectionClass::getPrototype이 가져온 Prototype의 메서드만 가져온 것처럼 보입니다.
이러한 차이점을 이해하기 위해 ReflectionMethod::getPrototype과 ReflectionClass::getPrototype의 차이점을 알려주실 수 있을까요?

    댓글목록

    profile_image
    나우호스팅  20시간 전



    ReflectionMethod::getPrototype과 ReflectionClass::getPrototype의 차이점은 Prototype을 가져오는 대상이 다르기 때문입니다.

    ReflectionMethod::getPrototype은 특정 메서드의 Prototype을 가져오기 때문에, 해당 메서드의 Prototype만 가져옵니다.

    반면, ReflectionClass::getPrototype은 클래스의 Prototype을 가져오기 때문에, 클래스의 모든 메서드와 속성을 포함한 Prototype을 가져옵니다.

    예를 들어, 다음 코드를 살펴보겠습니다.

    #hostingforum.kr
    php
    
    class A {
    
        public function method1() {}
    
        public function method2() {}
    
    }
    
    
    
    class B extends A {
    
        public function method2() {}
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('B');
    
    $reflectionMethod = new ReflectionMethod('B', 'method2');
    
    
    
    $prototype1 = $reflectionMethod->getPrototype();
    
    $prototype2 = $reflectionClass->getPrototype();
    
    
    
    print_r($prototype1);
    
    print_r($prototype2);
    
    


    이 코드를 실행하면, `$prototype1`에는 `B` 클래스의 `method2` 메서드만 포함된 Prototype이 포함되어 있습니다. 반면, `$prototype2`에는 `B` 클래스의 모든 메서드와 속성이 포함된 Prototype이 포함되어 있습니다.

    2025-06-23 20:24

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

검색

게시물 검색