개발자 Q&A

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

2025.03.21 15:26

ReflectionClass::hasProperty 관련 질문

목록
  • CI/CD장인 12시간 전 2025.03.21 15:26 새글
  • 1
    1
제가 ReflectionClass::hasProperty를 사용하여 프로퍼티가 존재하는지 확인하려고 하는데요.
이 메소드는 프로퍼티가 존재하는지 여부를 boolean값으로 반환하는데요.
이때, 프로퍼티의 접근 제어자에 따라 결과가 달라지는 것 같은데요.
어떤 접근 제어자가 프로퍼티를 보이게 할 수 있을까요?

    댓글목록

    profile_image
    나우호스팅  12시간 전



    ReflectionClass::hasProperty는 private, protected, static 프로퍼티를 확인할 때 false를 반환합니다.

    private 프로퍼티는 접근 제어자에 따라 보이게 할 수 없습니다.

    protected 프로퍼티는 자식 클래스에서 접근할 수 있습니다.

    static 프로퍼티는 클래스 이름을 통해 접근할 수 있습니다.

    예를 들어, 다음 코드는 private 프로퍼티를 확인할 때 false를 반환합니다.

    #hostingforum.kr
    php
    
    class Test {
    
        private $privateProperty;
    
    
    
        public function __construct() {
    
            $this->privateProperty = 'privateProperty';
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('Test');
    
    echo var_dump($reflection->hasProperty('privateProperty')); // bool(false)
    
    


    protected 프로퍼티를 확인할 때는 다음과 같이 접근할 수 있습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        protected $protectedProperty;
    
    
    
        public function __construct() {
    
            $this->protectedProperty = 'protectedProperty';
    
        }
    
    }
    
    
    
    class Child extends Test {
    
    }
    
    
    
    $reflection = new ReflectionClass('Child');
    
    echo var_dump($reflection->hasProperty('protectedProperty')); // bool(true)
    
    


    static 프로퍼티를 확인할 때는 다음과 같이 접근할 수 있습니다.

    #hostingforum.kr
    php
    
    class Test {
    
        public static $staticProperty;
    
    
    
        public function __construct() {
    
            self::$staticProperty = 'staticProperty';
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('Test');
    
    echo var_dump($reflection->hasProperty('staticProperty')); // bool(true)
    
    

    2025-03-21 15:27

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

검색

게시물 검색