
ReflectionProperty::isProtected 메서드는 프로퍼티가 보호되었는지 확인하는 데 사용됩니다.
프로퍼티가 보호되었는지 확인하기 위해 isProtected 메서드를 사용하는 조건은 다음과 같습니다.
- 프로퍼티가 보호된 접근 제어자 (protected, private)로 선언되어 있어야 합니다.
- 프로퍼티가 getter나 setter 메서드를 통해 접근되는 경우, 프로퍼티 자체가 보호된 접근 제어자로 선언되어 있어야 합니다.
예를 들어, 다음 코드는 프로퍼티가 보호된 접근 제어자로 선언되어 있으므로, isProtected 메서드는 true를 반환합니다.
#hostingforum.kr
php
class MyClass {
protected $myProperty;
public function getMyProperty() {
return $this->myProperty;
}
}
$reflection = new ReflectionClass('MyClass');
$property = $reflection->getProperty('myProperty');
echo $property->isProtected() ? 'true' : 'false'; // true
반면에, 다음 코드는 프로퍼티가 보호된 접근 제어자로 선언되어 있지 않으므로, isProtected 메서드는 false를 반환합니다.
#hostingforum.kr
php
class MyClass {
public $myProperty;
public function getMyProperty() {
return $this->myProperty;
}
}
$reflection = new ReflectionClass('MyClass');
$property = $reflection->getProperty('myProperty');
echo $property->isProtected() ? 'true' : 'false'; // false
따라서, 위 코드는 제대로 동작합니다.
2025-08-03 11:42