개발자 Q&A

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

2025.06.26 21:12

ReflectionClass::getProperties 함수 이해 도움이 필요합니다.

목록
  • Lisp전문가 23일 전 2025.06.26 21:12
  • 18
    1
저는 PHP의 ReflectionClass::getProperties 함수에 관해 공부중인데, 사용법을 헷갈리게 되었습니다.

getProperties 함수는 ReflectionClass 객체의 속성을 반환하는 함수입니다. 하지만 이 함수는 private 속성을 반환하지 않아도 된다고 하는데, 이 경우 private 속성을 반환할 수 있는 방법은 없나요?

또한, getProperties 함수에서 property_info 변수의 값을 변경할 수 있는 방법은 없나요? property_info 변수는 ReflectionProperty 객체의 정보를 담고 있습니다.

혹시 이러한 문제를 해결할 방법이 있나요?

    댓글목록

    profile_image
    나우호스팅  23일 전



    ReflectionClass::getProperties 함수는 private 속성을 반환하지 않습니다. 하지만 private 속성을 반환하고 싶다면, private 속성을 public 속성으로 변경하거나, getter/setter 메소드를 사용하여 접근할 수 있습니다.

    private 속성을 public 속성으로 변경하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $privateProperty;
    
    
    
        public function __construct() {
    
            $this->privateProperty = 'private value';
    
        }
    
    
    
        public function getPrivateProperty() {
    
            return $this->privateProperty;
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('Test');
    
    $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    
    
    foreach ($properties as $property) {
    
        $property->setAccessible(true);
    
        echo $property->getName() . ': ' . $property->getValue(new Test()) . "n";
    
    }
    
    


    getter/setter 메소드를 사용하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $privateProperty;
    
    
    
        public function __construct() {
    
            $this->privateProperty = 'private value';
    
        }
    
    
    
        public function getPrivateProperty() {
    
            return $this->privateProperty;
    
        }
    
    
    
        public function setPrivateProperty($value) {
    
            $this->privateProperty = $value;
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('Test');
    
    $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    
    
    foreach ($properties as $property) {
    
        $property->setAccessible(true);
    
        if ($property->getName() == 'privateProperty') {
    
            echo $property->getName() . ': ' . $property->getValue(new Test()) . "n";
    
            $property->setValue(new Test(), 'new value');
    
            echo $property->getName() . ': ' . $property->getValue(new Test()) . "n";
    
        }
    
    }
    
    


    property_info 변수의 값을 변경할 수 있는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $privateProperty;
    
    
    
        public function __construct() {
    
            $this->privateProperty = 'private value';
    
        }
    
    
    
        public function getPrivateProperty() {
    
            return $this->privateProperty;
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('Test');
    
    $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    
    
    foreach ($properties as $property) {
    
        $property->setAccessible(true);
    
        if ($property->getName() == 'privateProperty') {
    
            echo $property->getName() . ': ' . $property->getValue(new Test()) . "n";
    
            $property->setAccessible(false);
    
            $property->setAccessible(true);
    
            echo $property->getName() . ': ' . $property->getValue(new Test()) . "n";
    
        }
    
    }
    
    


    위의 예제에서 property_info 변수의 값을 변경할 수 있는 방법은 ReflectionProperty::setAccessible() 메소드를 사용하여 접근할 수 있습니다.

    2025-06-26 21:13

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

검색

게시물 검색