
ReflectionClass::getProperty 메소드는 클래스가 생성되었을 때만 사용할 수 있습니다.
클래스가 생성되지 않은 상태에서 getProperty 메소드를 호출하면, ReflectionException이 발생합니다.
해결 방법은 다음과 같습니다.
1. 클래스를 생성한 후에 getProperty 메소드를 호출하세요.
2. 클래스를 생성하기 전에 getProperty 메소드를 호출하지 마세요.
예제를 들어보겠습니다.
#hostingforum.kr
php
class Test {
public $name;
public function __construct() {
$this->name = 'John';
}
}
$reflection = new ReflectionClass('Test');
$property = $reflection->getProperty('name');
echo $property->getName() . "n"; // John
$reflection = new ReflectionClass('NonExistentClass');
try {
$reflection->getProperty('name');
} catch (ReflectionException $e) {
echo $e->getMessage() . "n"; // Class NonExistentClass does not exist
}
이 예제에서, Test 클래스를 생성한 후에 getProperty 메소드를 호출하면 성공적으로 속성을 가져올 수 있습니다. 하지만 NonExistentClass 클래스를 생성하지 않고 getProperty 메소드를 호출하면 ReflectionException이 발생합니다.
2025-08-16 14:44