라이브러리
[PHP] ReflectionProperty::isProtected - 속성이 보호되는지 확인합니다.
ReflectionProperty::isProtected
PHP Reflection API는 클래스의 속성을 분석하고 조작할 수 있도록 도와주는 도구입니다. ReflectionProperty 클래스는 클래스의 속성을 나타내며, 속성의 접근제어자를 확인할 수 있습니다. ReflectionProperty::isProtected 메소드는 속성이 보호된 속성인지 여부를 확인합니다.
예제
#hostingforum.kr
php
class User {
protected $name;
public $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
}
$user = new User('John Doe', 'john@example.com');
$reflectionClass = new ReflectionClass('User');
$reflectionProperty = $reflectionClass->getProperty('name');
echo $reflectionProperty->isPublic() ? 'public' : ($reflectionProperty->isProtected() ? 'protected' : 'private');
// 출력: protected
echo "
";
$reflectionProperty = $reflectionClass->getProperty('email');
echo $reflectionProperty->isPublic() ? 'public' : ($reflectionProperty->isProtected() ? 'protected' : 'private');
// 출력: public
위 예제에서, User 클래스의 name 속성은 보호된 속성입니다. ReflectionProperty::isProtected 메소드는 true를 반환합니다. 반면, email 속성은 공개 속성입니다. ReflectionProperty::isPublic 메소드는 true를 반환합니다.
접근제어자
PHP에서 속성은 다음과 같은 접근제어자를 가질 수 있습니다.
- `public` : 모든 클래스에서 접근할 수 있습니다.
- `protected` : 동일한 클래스 또는 상속된 클래스에서 접근할 수 있습니다.
- `private` : 동일한 클래스에서만 접근할 수 있습니다.
ReflectionProperty::isProtected 메소드는 속성이 보호된 속성인지 여부를 확인합니다. 만약 속성이 보호된 속성이라면 true를 반환하고, 아니라면 false를 반환합니다.
참고
ReflectionProperty::isProtected 메소드는 속성이 보호된 속성인지 여부를 확인합니다. 하지만, PHP 7.2 이상에서는 ReflectionProperty::isProtected 메소드가 deprecated되었으며, 대신 ReflectionProperty::isPrivate() 메소드를 사용해야 합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.