
ReflectionProperty::getAttributes 메서드는 클래스의 속성을 가져올 때 모든 속성을 가져올 수 있습니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
class User {
public $name;
public $email;
public $password;
}
$user = new User();
$reflection = new ReflectionClass($user);
$attributes = $reflection->getAttributes();
print_r($attributes);
위 코드에서 `$reflection->getAttributes()` 메서드는 모든 속성을 가져올 수 있습니다.
만약 특정 속성만 가져올 수 있는 방법을 알고 싶다면, ReflectionProperty 클래스의 getProperty 메서드를 사용할 수 있습니다.
#hostingforum.kr
php
class User {
public $name;
public $email;
public $password;
}
$user = new User();
$reflection = new ReflectionClass($user);
$nameProperty = $reflection->getProperty('name');
$emailProperty = $reflection->getProperty('email');
print_r($nameProperty);
print_r($emailProperty);
또한, ReflectionClass의 getProperties 메서드를 사용하여 특정 속성만 가져올 수 있습니다.
#hostingforum.kr
php
class User {
public $name;
public $email;
public $password;
}
$user = new User();
$reflection = new ReflectionClass($user);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
print_r($property);
}
위 코드에서 getProperties 메서드는 IS_PUBLIC, IS_PROTECTED, IS_PRIVATE 옵션을 사용하여 속성의 접근 제어자를 지정할 수 있습니다.
예를 들어, IS_PUBLIC 옵션을 사용하여 public 속성만 가져올 수 있습니다.
#hostingforum.kr
php
class User {
public $name;
protected $email;
private $password;
}
$user = new User();
$reflection = new ReflectionClass($user);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
print_r($property);
}
위 코드에서 getProperties 메서드는 public 속성인 $name만 가져올 수 있습니다.
2025-05-21 09:11