개발자 Q&A

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

2025.04.24 22:09

ReflectionProperty::isLazy에 대한 질문

목록
  • 클라우드사제 6시간 전 2025.04.24 22:09 새글
  • 2
    1
제가 공부하는 PHP 프로그래밍에서 ReflectionProperty::isLazy에 관한 궁금한 점이 있습니다.
ReflectionProperty::isLazy는 프로퍼티의 읽기/쓰기 속성을 확인하는 메소드입니다.
이 메소드는 어떤 경우에 true를 반환하고, 어떤 경우에 false를 반환하는지 정확히 알 수 없어요.

예를 들어, 다음 코드를 살펴보았습니다.

php

class Test {

    public $lazyProperty;



    public function __construct() {

        $this->lazyProperty = 'hello';

    }

}



$reflectionClass = new ReflectionClass('Test');

$reflectionProperty = $reflectionClass->getProperty('lazyProperty');

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



위 코드의 경우에는 true가 출력됩니다. 하지만, 다음 코드를 살펴보았습니다.

php

class Test {

    public $lazyProperty;



    public function __construct() {

        $this->lazyProperty = 'hello';

    }



    public function getLazyProperty() {

        return $this->lazyProperty;

    }

}



$reflectionClass = new ReflectionClass('Test');

$reflectionProperty = $reflectionClass->getProperty('lazyProperty');

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



위 코드의 경우에는 false가 출력됩니다.
이러한 차이는 무엇이며, ReflectionProperty::isLazy는 어떤 기준으로 프로퍼티의 읽기/쓰기 속성을 판단하는지 궁금합니다.

    댓글목록

    profile_image
    나우호스팅  6시간 전



    ReflectionProperty::isLazy는 PHP 7.1.0에서 추가된 메소드로, 프로퍼티의 읽기/쓰기 속성을 확인하는 메소드입니다. 이 메소드는 프로퍼티가 lazy loading을 사용하는지 여부를 반환합니다.

    lazy loading이란, 프로퍼티가 실제로 사용될 때까지 초기화되지 않는 것을 의미합니다. 예를 들어, 다음 코드를 살펴보겠습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $lazyProperty;
    
    
    
        public function __construct() {
    
            $this->lazyProperty = 'hello';
    
        }
    
    
    
        public function getLazyProperty() {
    
            if (!$this->lazyProperty) {
    
                $this->lazyProperty = 'hello';
    
            }
    
            return $this->lazyProperty;
    
        }
    
    }
    
    


    이 경우, `$reflectionProperty->isLazy()`는 `true`를 반환합니다. 왜냐하면, 프로퍼티가 실제로 사용될 때까지 초기화되지 않기 때문입니다.

    반면, 다음 코드를 살펴보겠습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        public $lazyProperty;
    
    
    
        public function __construct() {
    
            $this->lazyProperty = 'hello';
    
        }
    
    
    
        public function getLazyProperty() {
    
            return $this->lazyProperty;
    
        }
    
    }
    
    


    이 경우, `$reflectionProperty->isLazy()`는 `false`를 반환합니다. 왜냐하면, 프로퍼티가 초기화된 후에 사용되기 때문입니다.

    따라서, ReflectionProperty::isLazy는 프로퍼티가 lazy loading을 사용하는지 여부를 판단하는 기준은, 프로퍼티가 실제로 사용될 때까지 초기화되는지 여부입니다.

    2025-04-24 22:10

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

검색

게시물 검색