
DateTimeImmutable::createFromInterface 메서드는 인터페이스 인스턴스로부터 DateTimeImmutable 객체를 생성하는 데 사용할 수 없습니다.
이 메서드는 DateTimeInterface 인터페이스 인스턴스에서 DateTimeImmutable 객체를 생성하는 데 사용할 수 있습니다.
예를 들어, DateTimeInterface 인터페이스에 정의된 getCurrentDateTime() 메서드를 호출하여 DateTimeImmutable 객체를 생성할 수 있습니다.
#hostingforum.kr
php
interface DateTimeInterface {
public function getCurrentDateTime(): DateTimeImmutable;
}
class MyDateTime implements DateTimeInterface {
public function getCurrentDateTime(): DateTimeImmutable {
return DateTimeImmutable::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s'));
}
}
$myDateTime = new MyDateTime();
$date = $myDateTime->getCurrentDateTime();
또한, DateTimeInterface 인터페이스에 정의된 formats() 메서드를 호출하여 DateTimeImmutable 객체를 생성할 수 있습니다.
#hostingforum.kr
php
interface DateTimeInterface {
public function formats(): array;
}
class MyDateTime implements DateTimeInterface {
public function formats(): array {
return ['Y-m-d H:i:s'];
}
}
$myDateTime = new MyDateTime();
$date = DateTimeImmutable::createFromFormat($myDateTime->formats()[0], date($myDateTime->formats()[0]));
2025-06-21 04:33