
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