
jsonSerialize 메서드를 호출할 때 decimal128 타입의 데이터를 string 타입으로 변환하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$decimal128 = new MongoDBBSONDecimal128('123.456');
$json = $decimal128->jsonSerialize();
print_r($json);
// string 타입으로 변환
$string = (string) $decimal128;
print_r($string);
jsonSerialize 메서드의 오버로드를 사용하여 decimal128 타입의 데이터를 다른 타입으로 변환하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$decimal128 = new MongoDBBSONDecimal128('123.456');
// int 타입으로 변환
$int = (int) $decimal128;
print_r($int);
// float 타입으로 변환
$float = (float) $decimal128;
print_r($float);
// string 타입으로 변환
$string = (string) $decimal128;
print_r($string);
jsonSerialize 메서드의 반환 타입이 jsonSerializable 타입이기 때문에, jsonSerializable 타입의 객체를 만들고 jsonSerialize 메서드를 호출하여 json 포맷의 데이터를 생성하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$decimal128 = new MongoDBBSONDecimal128('123.456');
// jsonSerializable 타입의 객체를 만들고 jsonSerialize 메서드를 호출
$jsonSerializable = new class {
public function jsonSerialize() {
return $decimal128->jsonSerialize();
}
};
$json = $jsonSerializable->jsonSerialize();
print_r($json);
위의 코드를 실행하면 json 포맷의 데이터가 생성되는 것을 확인할 수 있습니다.
2025-05-25 18:22