개발자 Q&A

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

2025.06.05 20:13

ReflectionProperty::getAttributes에 대한 궁금함

목록
  • 제로데이헌터 1일 전 2025.06.05 20:13
  • 3
    1
저는 ReflectionProperty::getAttributes를 사용하여 클래스의 속성을 반영하는 방법을 공부 중입니다.
하지만 ReflectionProperty::getAttributes의 반환 타입에 대해 이해가 부족합니다.
getAttributes는 ReflectionProperty의 속성이 어떤 타입으로 반환되는지 궁금합니다.

    댓글목록

    profile_image
    나우호스팅  1일 전



    ReflectionProperty::getAttributes 메소드는 ReflectionAttribute[] 타입의 값을 반환합니다.

    ReflectionAttribute는 클래스의 속성을 반영하는 데 사용되는 속성의 정보를 포함하는 클래스입니다.

    예를 들어, 클래스에 @property 어노테이션을 사용하여 속성을 정의한 경우, ReflectionAttribute를 통해 속성의 정보를 가져올 수 있습니다.

    예시:

    #hostingforum.kr
    php
    
    class MyClass {
    
        /**
    
         * @property int $myProperty
    
         */
    
        public $myProperty;
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $reflectionProperty = $reflectionClass->getProperty('myProperty');
    
    $attributes = $reflectionProperty->getAttributes();
    
    
    
    foreach ($attributes as $attribute) {
    
        echo $attribute->getName() . "n";
    
        echo $attribute->getTarget() . "n";
    
    }
    
    

    위 예시에서는 MyClass 클래스의 myProperty 속성에 대한 ReflectionAttribute를 가져와 이름과 타겟을 출력합니다.

    이러한 방법으로 ReflectionProperty::getAttributes 메소드를 사용하여 클래스의 속성을 반영할 수 있습니다.

    2025-06-05 20:14

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

검색

게시물 검색