개발자 Q&A

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

2025.08.03 04:40

ReflectionClass::getProperties와 getPropertiesNames의 차이점을 알려주세요?

목록
  • HTTP전문가 10시간 전 2025.08.03 04:40 새글
  • 1
    1



    댓글목록

    profile_image
    나우호스팅  10시간 전



    ReflectionClass::getProperties와 getPropertiesNames의 차이점은 다음과 같습니다.

    - ReflectionClass::getProperties는 클래스의 모든 속성을 반환합니다. 속성은 이름과 값, 타입을 포함한 정보를 반환합니다.
    - ReflectionClass::getPropertiesNames는 클래스의 모든 속성 이름을 반환합니다. 속성 이름만 반환합니다.

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

    #hostingforum.kr
    php
    
    class TestClass {
    
        public $name = 'John';
    
        public $age = 30;
    
        private $email = 'john@example.com';
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('TestClass');
    
    $properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    foreach ($properties as $property) {
    
        echo $property->getName() . ': ' . $property->getValue(new TestClass()) . "n";
    
    }
    
    
    
    $propertyNames = $reflectionClass->getPropertiesNames();
    
    foreach ($propertyNames as $propertyName) {
    
        echo $propertyName . "n";
    
    }
    
    


    이 코드를 실행하면, ReflectionClass::getProperties는 모든 속성 이름과 값을 반환하고, ReflectionClass::getPropertiesNames는 모든 속성 이름만 반환합니다.

    2025-08-03 04:41

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

검색

게시물 검색