
ReflectionClass::getProperty 메서드는 클래스의 속성을 접근하는 데 사용됩니다. 이 메서드의 반환값은 ReflectionProperty 클래스의 인스턴스입니다.
ReflectionProperty 클래스는 stdClass와 다르게 ReflectionProperty 클래스의 속성을 가질 수 있습니다.
속성은 다음과 같습니다.
- getName(): 속성 이름을 반환합니다.
- getType(): 속성 타입을 반환합니다.
- getDeclaringClass(): 속성을 선언한 클래스를 반환합니다.
- getDefaultValue(): 속성의 기본값을 반환합니다.
- isPublic(): 속성이 퍼블릭인지 여부를 반환합니다.
- isPrivate(): 속성이 프라이빗인지 여부를 반환합니다.
- isProtected(): 속성이 보호된지 여부를 반환합니다.
- getModifiers(): 속성의 모디파이어를 반환합니다.
Person 클래스의 이름 속성을 접근하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$reflectionClass = new ReflectionClass('Person');
$property = $reflectionClass->getProperty('name');
$property->setAccessible(true); // 퍼블릭이 아닌 속성을 접근하기 위해 setAccessible 메서드를 사용합니다.
$person = new Person();
echo $property->getValue($person);
위 코드는 Person 클래스의 이름 속성을 접근할 수 있습니다. `$property` 변수의 타입은 ReflectionProperty 클래스의 인스턴스입니다.
2025-07-05 06:55