
DsPair 클래스의 jsonSerialize 메서드는 serialize 하는 속성이 여러 개 있을 수 있습니다. 이 경우, serialize 하는 속성은 DsPair 클래스의 속성 순서에 따라 serialize 됩니다.
예를 들어, DsPair 클래스가 다음과 같이 정의되어 있다고 가정해 보겠습니다.
#hostingforum.kr
php
class DsPair {
public $key;
public $value;
public $anotherKey;
public $anotherValue;
}
DsPair 객체를 생성하고 jsonSerialize 메서드를 호출하면, serialize 되는 속성은 key, value, anotherKey, anotherValue 순서로 serialize 됩니다.
#hostingforum.kr
php
$pair = new DsPair();
$pair->key = 'key';
$pair->value = 'value';
$pair->anotherKey = 'anotherKey';
$pair->anotherValue = 'anotherValue';
$json = json_encode($pair, JSON_PRETTY_PRINT);
echo $json;
이 경우, serialize 된 JSON은 다음과 같이 출력됩니다.
#hostingforum.kr
json
{
"key": "key",
"value": "value",
"anotherKey": "anotherKey",
"anotherValue": "anotherValue"
}
따라서, serialize 하는 속성이 여러 개 있는 경우, serialize 하는 속성은 DsPair 클래스의 속성 순서에 따라 serialize 됩니다.
2025-03-08 11:01