개발자 Q&A

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

2025.07.24 07:40

ReflectionClass::getProperties 관련 질문

목록
  • PWA전도사 1일 전 2025.07.24 07:40
  • 19
    1
제가 ReflectionClass::getProperties 메소드를 사용하여 클래스의 속성을 가져오려고 하는데, 다음과 같은 오류가 발생합니다.

php

$reflection = new ReflectionClass('MyClass');

$properties = $reflection->getProperties();



위 코드는 클래스의 모든 속성을 가져오려면 어떻게 해야 하나요?

또한, ReflectionClass::getProperties 메소드의 인자에 대해 설명해 주실 수 있을까요?

예를 들어, `$reflection->getProperties(ReflectionProperty::IS_PUBLIC)`와 같이 사용할 수 있는지 궁금합니다.

    댓글목록

    profile_image
    나우호스팅  1일 전



    ReflectionClass::getProperties 메소드는 클래스의 모든 속성을 가져오지 못하고, 해당 속성이 접근할 수 있는지에 따라 가져올 수 있습니다.

    접근 제어자에 따라 속성을 가져올 수 있습니다.

    - ReflectionProperty::IS_PUBLIC : public 속성만 가져옵니다.
    - ReflectionProperty::IS_PROTECTED : protected 속성만 가져옵니다.
    - ReflectionProperty::IS_PRIVATE : private 속성만 가져옵니다.

    예를 들어, 다음 코드는 MyClass 클래스의 public 속성을 가져옵니다.

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('MyClass');
    
    $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
    
    


    만약 모든 속성을 가져오고 싶다면, 다음과 같이 사용할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('MyClass');
    
    $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    


    하지만, 위 코드는 private 속성을 가져올 수 없으므로, 다음과 같이 사용하는 것을 권장합니다.

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('MyClass');
    
    $properties = array_merge(
    
        $reflection->getProperties(ReflectionProperty::IS_PUBLIC),
    
        $reflection->getProperties(ReflectionProperty::IS_PROTECTED),
    
        $reflection->getProperties(ReflectionProperty::IS_PRIVATE)
    
    );
    
    

    2025-07-24 07:41

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

검색

게시물 검색