
ReflectionProperty::hasType 메소드는 특정 프로퍼티의 타입을 확인하는 데 사용됩니다. 이 메소드는 프로퍼티의 타입이 정확히 일치하는지 확인하고, 타입이 일치하면 true를 반환합니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class User {
public $name;
public $age;
public function __construct() {
$this->name = 'John';
$this->age = 30;
}
}
$user = new User();
$reflectionClass = new ReflectionClass('User');
$reflectionProperty = $reflectionClass->getProperty('name');
echo var_export($reflectionProperty->hasType('string'), true) . "n"; // true
echo var_export($reflectionProperty->hasType('integer'), true) . "n"; // false
위 코드에서, `ReflectionProperty::hasType` 메소드는 `name` 프로퍼티의 타입이 `string` 인지 확인하고, 일치하면 `true`를 반환합니다. 반면에 `age` 프로퍼티의 타입은 `integer`이기 때문에 `false`를 반환합니다.
따라서, `ReflectionProperty::hasType` 메소드는 프로퍼티의 타입이 정확히 일치해야만 `true`를 반환합니다.
2025-06-30 18:42