
DateTimeImmutable::createFromMutable 메소드는 Mutable 객체를 사용하여 DateTimeImmutable 객체를 생성합니다. 그러나 생성된 DateTimeImmutable 객체는 Immutable 객체이므로, timestamp 값을 변경할 수 없습니다. Immutable 객체는 내부 상태를 변경할 수 없기 때문입니다.
Mutable 객체를 사용하여 생성한 DateTimeImmutable 객체는 읽기 전용으로 사용해야 하며, timestamp 값을 변경하려고 하면 Exception이 발생합니다.
예를 들어, 다음과 같은 코드를 실행하면 Exception이 발생합니다.
#hostingforum.kr
php
$mutable = new DateTime('2022-01-01 00:00:00');
$immutable = DateTimeImmutable::createFromMutable($mutable);
$immutable->modify('+1 day'); // Exception 발생
2025-08-06 02:07