개발자 Q&A

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

2025.07.13 21:49

ReflectionAttribute::getTarget에 대한 이해

목록
  • 코딩테스트러버 18시간 전 2025.07.13 21:49 새글
  • 4
    1
제가 ReflectionAttribute::getTarget에 대해 이해가 되지 않습니다.
ReflectionAttribute::getTarget는 어떤 역할을 하는 것인지 설명해 주실 수 있나요?
특히, ReflectionAttribute::getTarget를 사용하여 클래스의 속성을 얻을 수 있는 방법은 무엇인지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  18시간 전



    ReflectionAttribute::getTarget는 속성에 대한 정보를 담고 있는 Attribute를 가져오는 메서드입니다. Attribute는 클래스나 메서드에 대한 추가 정보를 제공하는 데 사용됩니다.

    클래스의 속성을 얻기 위해서는 ReflectionAttribute::getTarget를 사용하여 Attribute를 가져오고, 그 속성을 통해 클래스의 속성을 얻을 수 있습니다.

    예를 들어, 다음 코드는 클래스의 속성을 얻는 방법을 보여줍니다.

    #hostingforum.kr
    php
    
    use ReflectionClass;
    
    use ReflectionProperty;
    
    
    
    class MyClass {
    
        public $myProperty = 'Hello, World!';
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $reflectionProperty = $reflectionClass->getProperty('myProperty');
    
    $attribute = $reflectionProperty->getAttributes()[0];
    
    $target = $attribute->getTarget();
    
    
    
    echo $target->getName(); // MyClass
    
    echo $target->getDeclaringClass()->getName(); // MyClass
    
    echo $target->getValue(new MyClass()); // Hello, World!
    
    


    위 코드에서 ReflectionAttribute::getTarget를 사용하여 MyClass의 myProperty 속성을 얻을 수 있습니다.

    2025-07-13 21:50

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

검색

게시물 검색