개발자 Q&A

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

2025.04.25 05:28

ReflectionProperty::isReadOnly와 관련하여 질문내용입니다.

목록
  • UI디자이너 11시간 전 2025.04.25 05:28 새글
  • 3
    1
ReflectionProperty::isReadOnly와 로드 온리 속성

제가 ReflectionProperty::isReadOnly를 사용하여 로드 온리 속성을 확인하고 있습니다. 하지만 로드 온리 속성이 true인 경우에만 isReadOnly가 true가 되는 것을 알게되었습니다. 로드 온리 속성이 false인 경우에도 isReadOnly가 true인 상황이 발생하는 것 같습니다. 로드 온리 속성이 false인 경우에도 isReadOnly가 true인 이유를 알고 싶습니다.

예를 들어, 다음 코드를 통해 로드 온리 속성이 false인 경우에도 isReadOnly가 true인 것을 확인할 수 있습니다.

php

$reflectionProperty = new ReflectionProperty('stdClass', 'test');

$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue(new stdClass(), 'value');



echo $reflectionProperty->isReadOnly() ? 'true' : 'false'; // true



$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue(new stdClass(), null);



echo $reflectionProperty->isReadOnly() ? 'true' : 'false'; // true



위 코드에서 로드 온리 속성이 false인 경우에도 isReadOnly가 true인 것을 볼 수 있습니다. 로드 온리 속성이 false인 경우에도 isReadOnly가 true인 이유를 알고 싶습니다.

    댓글목록

    profile_image
    나우호스팅  11시간 전



    로드 온리 속성이 false인 경우에도 isReadOnly가 true인 이유는 ReflectionProperty::isReadOnly가 속성의 읽기 전용 여부를 확인하는 것이 아니라, 속성이 실제로 읽기 전용인지 여부를 확인하기 때문입니다.

    속성이 읽기 전용이 아닌 경우에도 isReadOnly가 true가 될 수 있는 이유는 속성이 private 또는 protected로 선언된 경우에 속성이 읽기 전용으로 처리되는 경우가 있기 때문입니다.

    예를 들어, 다음 코드를 통해 private 속성이 읽기 전용으로 처리되는 것을 확인할 수 있습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $test;
    
    
    
        public function __construct() {
    
            $this->test = 'value';
    
        }
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('Test');
    
    $reflectionProperty = $reflectionClass->getProperty('test');
    
    $reflectionProperty->setAccessible(true);
    
    
    
    echo $reflectionProperty->isReadOnly() ? 'true' : 'false'; // true
    
    


    또한, 속성이 final로 선언된 경우에도 속성이 읽기 전용으로 처리되는 경우가 있습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        final public $test;
    
    
    
        public function __construct() {
    
            $this->test = 'value';
    
        }
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('Test');
    
    $reflectionProperty = $reflectionClass->getProperty('test');
    
    $reflectionProperty->setAccessible(true);
    
    
    
    echo $reflectionProperty->isReadOnly() ? 'true' : 'false'; // true
    
    


    따라서, ReflectionProperty::isReadOnly가 true인 경우에는 속성이 실제로 읽기 전용인지 여부를 확인해야 합니다.

    2025-04-25 05:29

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

검색

게시물 검색