개발자 Q&A

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

2025.04.15 07:51

ReflectionClass::인스턴스 변수 이름에 네임스페이스를 포함하는 방법은 무엇인가요?

목록
  • 네트워크마법사 1일 전 2025.04.15 07:51
  • 15
    1
제가 ReflectionClass::getProperties() 메소드를 사용하여 인스턴스 변수를 얻을 때, 네임스페이스가 포함된 변수 이름을 얻을 수 있는 방법을 모르겠습니다.

예를 들어, 내 클래스는 다음과 같이 네임스페이스를 사용한 인스턴스 변수를 가지고 있습니다.

php

class MyClass {

    public function __construct() {

        $this->NamespaceVariable1 = '값1';

        $this->NamespaceVariable2 = '값2';

    }

}



이때, ReflectionClass::getProperties() 메소드를 사용하여 인스턴스 변수를 얻을 때, 네임스페이스가 포함된 변수 이름을 얻을 수 있는 방법을 알고 싶습니다.

    댓글목록

    profile_image
    나우호스팅  1일 전



    네임스페이스를 포함한 인스턴스 변수 이름을 얻으려면, ReflectionClass::getProperties() 메소드의 두 번째 인자 $object에 ReflectionObject를 전달해야 합니다.

    예를 들어, 다음과 같이 사용할 수 있습니다.

    #hostingforum.kr
    php
    
    class MyClass {
    
        public function __construct() {
    
            $this->Namespace\Variable1 = '값1';
    
            $this->Namespace\Variable2 = '값2';
    
        }
    
    }
    
    
    
    $obj = new MyClass();
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE, $obj);
    
    
    
    foreach ($properties as $property) {
    
        $propertyName = $property->getName();
    
        echo $propertyName . "n";
    
    }
    
    


    이 코드를 실행하면, 네임스페이스를 포함한 인스턴스 변수 이름인 Namespace\Variable1과 Namespace\Variable2가 출력됩니다.

    2025-04-15 07:52

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

검색

게시물 검색