라이브러리
[PHP] DateTimeImmutable::createFromMutable - 지정된 DateTime 객체를 캡슐화하는 새 DateTimeImmutable 인스턴스를 반환합니다.
PHP 8.1 버전부터 `DateTimeImmutable` 클래스가 추가되었습니다. 이 클래스는 `DateTime` 클래스와 유사하지만, 생성된 객체를 변경할 수 없습니다.
`DateTimeImmutable` 클래스의 `createFromMutable` 메서드는 `DateTime` 클래스의 객체를 `DateTimeImmutable` 클래스의 객체로 변환하는 데 사용됩니다.
DateTimeImmutable::createFromMutable 메서드
`DateTimeImmutable::createFromMutable` 메서드는 `DateTime` 클래스의 객체를 `DateTimeImmutable` 클래스의 객체로 변환합니다. 이 메서드는 `DateTime` 클래스의 객체를 생성하고, 그 객체를 `DateTimeImmutable` 클래스의 객체로 변환하여 반환합니다.
사용 예제
#hostingforum.kr
php
// DateTime 클래스의 객체를 생성합니다.
$dateTime = new DateTime('2022-01-01 12:00:00');
// DateTimeImmutable 클래스의 객체를 생성합니다.
$dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime);
// DateTimeImmutable 클래스의 객체를 출력합니다.
echo $dateTimeImmutable->format('Y-m-d H:i:s') . "
";
// DateTimeImmutable 클래스의 객체를 변경할 수 없습니다.
try {
$dateTimeImmutable->modify('+1 day');
} catch (Exception $e) {
echo $e->getMessage() . "
";
}
결과
#hostingforum.kr
2022-01-01 12:00:00
DateTimeImmutable::__construct(): Cannot modify immutable DateTimeImmutable object
사용법
1. `DateTime` 클래스의 객체를 생성합니다.
2. `DateTimeImmutable::createFromMutable` 메서드를 사용하여 `DateTime` 클래스의 객체를 `DateTimeImmutable` 클래스의 객체로 변환합니다.
3. `DateTimeImmutable` 클래스의 객체를 사용합니다.
참고
* `DateTimeImmutable` 클래스는 `DateTime` 클래스와 유사하지만, 생성된 객체를 변경할 수 없습니다.
* `DateTimeImmutable::createFromMutable` 메서드는 `DateTime` 클래스의 객체를 `DateTimeImmutable` 클래스의 객체로 변환합니다.
* `DateTimeImmutable` 클래스의 객체를 변경할 수 없습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.