개발자 Q&A

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

2025.04.06 15:50

ReflectionClass::newLazyProxy에 대한 질문

목록
  • 리트코드광 2일 전 2025.04.06 15:50
  • 2
    1
저는 ReflectionClass::newLazyProxy를 사용하여 객체를 생성할 때 발생하는 이슈에 대해 도움을 받고 싶습니다.

저는 ReflectionClass::newLazyProxy를 사용하여 객체를 생성할 때, 객체의 속성에 접근할 수 없을 때가 있습니다.

예를 들어, 다음과 같은 코드를 작성했습니다.

php

$reflectionClass = new ReflectionClass('MyClass');

$lazyProxy = $reflectionClass->newLazyProxy();



그런 다음, `$lazyProxy`를 사용하여 `MyClass`의 속성을 접근하려고 합니다.

php

$property = $lazyProxy->getProperty('myProperty');



하지만, `$lazyProxy->getProperty('myProperty')`는 항상 `null`을 반환합니다.

어떻게 하면 `$lazyProxy`를 사용하여 `MyClass`의 속성을 접근할 수 있을까요?

혹시 어떤 속성이나 메소드를 사용해야 하는지 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  2일 전



    ReflectionClass::newLazyProxy를 사용하여 객체를 생성한 후, 객체의 속성을 접근할 수 없을 때는 두 가지 경우가 있습니다.

    1. 객체의 속성이 private이거나 private final이면, ReflectionClass::newLazyProxy를 사용하여 접근할 수 없습니다.
    2. 객체의 속성이 protected이면, ReflectionClass::newLazyProxy를 사용하여 접근할 수 없습니다.

    protected 속성을 접근하기 위해서는, ReflectionClass::newLazyProxy를 사용하여 접근할 수 없습니다. 대신, ReflectionClass::newInstance()를 사용하여 객체를 생성한 후, protected 속성을 접근할 수 있습니다.

    #hostingforum.kr
    php
    
        $reflectionClass = new ReflectionClass('MyClass);
    
        $instance = $reflectionClass->newInstance();
    
        $property = $instance->getProperty('myProperty');
    
        


    3. private final 속성을 접근하기 위해서는, ReflectionClass::newInstance()를 사용하여 객체를 생성한 후, private final 속성을 접근할 수 없습니다. 대신, private final 속성을 public으로 변경하거나, getter/setter 메소드를 추가하여 접근할 수 있습니다.

    #hostingforum.kr
    php
    
        class MyClass {
    
            private $myProperty;
    
    
    
            public function getMyProperty() {
    
                return $this->myProperty;
    
            }
    
    
    
            public function setMyProperty($value) {
    
                $this->myProperty = $value;
    
            }
    
        }
    
    
    
        $reflectionClass = new ReflectionClass('MyClass);
    
        $instance = $reflectionClass->newInstance();
    
        $property = $instance->getMyProperty();
    
        $instance->setMyProperty('value');
    
        

    2025-04-06 15:51

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

검색

게시물 검색