개발자 Q&A

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

2025.06.22 01:51

ReflectionProperty::getDocComment에 대한 이해를 도와주세요.

목록
  • 트랜잭션장인 오래 전 2025.06.22 01:51
  • 20
    1
제가 ReflectionProperty::getDocComment를 사용하여 클래스의 프로퍼티에 대한 설명을 가져오려고 하는데,

다음과 같은 코드를 실행했을 때, null이 반환되는 이유를 알려주세요.

php

$reflectionClass = new ReflectionClass('클래스명');

$property = $reflectionClass->getProperty('프로퍼티명');

$docComment = $property->getDocComment();



var_dump($docComment);



이때, 프로퍼티에 대한 설명이 존재하지만 null이 반환되는 이유를 알려주세요.

    댓글목록

    profile_image
    나우호스팅  오래 전



    ReflectionProperty::getDocComment() 메소드는 프로퍼티에 대한 설명을 가져올 때, 프로퍼티가 private 또는 protected 인 경우 null을 반환합니다.

    이유는 PHP에서 private 또는 protected 프로퍼티는 외부에서 직접 접근할 수 없기 때문입니다. ReflectionClass와 ReflectionProperty는 PHP의 내부 구조를 분석할 수 있지만, private 또는 protected 프로퍼티의 설명은 외부에서 접근할 수 없기 때문에 null을 반환합니다.

    프로퍼티가 public 인 경우, 프로퍼티에 대한 설명을 가져올 수 있습니다.

    예를 들어, 다음 코드를 실행했을 때, 프로퍼티에 대한 설명을 가져올 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('클래스명');
    
    $property = $reflectionClass->getProperty('프로퍼티명');
    
    if ($property->isPublic()) {
    
        $docComment = $property->getDocComment();
    
        var_dump($docComment);
    
    }
    
    


    이 코드는 프로퍼티가 public 인 경우에만 프로퍼티에 대한 설명을 가져옵니다.

    2025-06-22 01:52

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

검색

게시물 검색