
ReflectionProperty::isReadOnly는 PHP의 ReflectionProperty 클래스에서 사용할 수 있는 메서드 중 하나로, 특정 프로퍼티가 읽기 전용인지 아닌지를 확인하는 데 사용됩니다.
ReflectionProperty::isReadOnly는 프로퍼티가 읽기 전용이면 true를, 아니라면 false를 반환합니다. 읽기 전용 프로퍼티는 클래스에 선언된 프로퍼티에 접근 제한자를 사용하여 정의할 수 있습니다. 예를 들어, 클래스에 선언된 프로퍼티에 readonly 접근 제한자를 사용하면, 해당 프로퍼티는 읽기 전용이 됩니다.
#hostingforum.kr
php
class MyClass {
private readonly $readOnlyProperty;
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionProperty = $reflectionClass->getProperty('readOnlyProperty');
echo $reflectionProperty->isReadOnly(); // true
또한, PHP 8.1 이상부터는 final 키워드를 사용하여 프로퍼티를 읽기 전용으로 정의할 수 있습니다.
#hostingforum.kr
php
class MyClass {
public final $readOnlyProperty;
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionProperty = $reflectionClass->getProperty('readOnlyProperty');
echo $reflectionProperty->isReadOnly(); // true
반면, 읽기 전용이 아닌 프로퍼티의 경우, ReflectionProperty::isReadOnly는 false를 반환합니다.
#hostingforum.kr
php
class MyClass {
public $readWriteProperty;
}
$reflectionClass = new ReflectionClass('MyClass');
$reflectionProperty = $reflectionClass->getProperty('readWriteProperty');
echo $reflectionProperty->isReadOnly(); // false
2025-08-10 16:16