개발자 Q&A

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

2025.06.12 10:06

ReflectionProperty::getName() 메서드 사용에 대한 질문

목록
  • 배포요정 오래 전 2025.06.12 10:06 인기
  • 101
    1
저는 ReflectionProperty::getName() 메서드를 사용하여 클래스의 속성을 이름으로 가져오는 방법을 공부하고 있씁니다. 하지만, 속성이 없는 경우 메서드는 어떻게 동작하나요? 예를 들어, 다음 코드를 살펴보겠습니다.

php

class TestClass {

    public $testProperty;

}



$reflection = new ReflectionClass('TestClass');

$property = $reflection->getProperty('testProperty');



echo $property->getName(); // testProperty

echo "n";

echo $property->getName(); // testProperty



$reflection = new ReflectionClass('TestClass');

$property = $reflection->getProperty('nonExistentProperty');

echo $property->getName(); // 이 경우 어떻게 동작하나요?



속성이 없는 경우 getName() 메서드는 어떻게 동작하나요?

    댓글목록

    profile_image
    나우호스팅  오래 전



    속성이 없는 경우 ReflectionProperty::getName() 메서드는 PHP Notice: Undefined property: TestClass::$nonExistentProperty 오류를 발생시키지 않고, 속성이 존재하지 않음을 나타내는 빈 문자열을 반환합니다.

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

    #hostingforum.kr
    php
    
    class TestClass {
    
        public $testProperty;
    
    }
    
    
    
    $reflection = new ReflectionClass('TestClass');
    
    $property = $reflection->getProperty('nonExistentProperty');
    
    
    
    var_dump($property->getName()); // string(0) ""
    
    


    이러한 동작은 ReflectionProperty 클래스의 디자인 결정에 따라서 발생합니다. ReflectionProperty 클래스는 속성이 존재하지 않더라도 속성 이름을 반환하도록 설계되어 있습니다.

    2025-06-12 10:07

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

검색

게시물 검색