
ReflectionClass::getProperties를 사용하여 객체의 프로퍼티 정보를 얻은 후, 프로퍼티를 동적으로 추가하거나 수정하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$property->setAccessible(true);
if (!property_exists($person, $property->getName())) {
$person->$property->getName() = '';
}
// 프로퍼티 추가 또는 수정
$person->$property->getName() = '새로운 값';
}
프로퍼티를 삭제하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$property->setAccessible(true);
if (property_exists($person, $property->getName())) {
unset($person->$property->getName());
}
}
프로퍼티를 확인하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$property->setAccessible(true);
if (property_exists($person, $property->getName())) {
echo $property->getName() . '이 존재합니다.' . "n";
} else {
echo $property->getName() . '이 존재하지 않습니다.' . "n";
}
}
프로퍼티를 복사하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$property->setAccessible(true);
if (property_exists($person, $property->getName())) {
$copiedProperty = $person->$property->getName();
$person->$property->getName() = $copiedProperty;
}
}
프로퍼티를 비교하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
$property->setAccessible(true);
if (property_exists($person, $property->getName())) {
$value1 = $person->$property->getName();
$value2 = '비교할 값';
if ($value1 == $value2) {
echo $property->getName() . '이 일치합니다.' . "n";
} else {
echo $property->getName() . '이 일치하지 않습니다.' . "n";
}
}
}
프로퍼티를 정렬하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
uasort($properties, function($a, $b) {
return strcmp($a->getName(), $b->getName());
});
foreach ($properties as $property) {
$property->setAccessible(true);
echo $property->getName() . "n";
}
프로퍼티를 그룹화하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$groupedProperties = array();
foreach ($properties as $property) {
$property->setAccessible(true);
$groupName = substr($property->getName(), 0, 1);
if (!isset($groupedProperties[$groupName])) {
$groupedProperties[$groupName] = array();
}
$groupedProperties[$groupName][] = $property->getName();
}
foreach ($groupedProperties as $groupName => $properties) {
echo $groupName . ":n";
foreach ($properties as $property) {
echo " " . $property . "n";
}
}
이러한 방법들은 ReflectionClass::getProperties를 사용하여 객체의 프로퍼티 정보를 얻은 후, 프로퍼티를 동적으로 추가하거나 수정, 삭제, 확인, 복사, 비교, 정렬, 그룹화하는 방법을示しています.
2025-08-12 09:13