개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.06.21 04:33

DateTimeImmutable::createFromInterface 관련 질문

목록
  • GraphQL러버 13시간 전 2025.06.21 04:33 새글
  • 3
    1
DateTimeImmutable::createFromInterface 메서드는 인터페이스 인스턴스로부터 DateTimeImmutable 객체를 생성할 수 있는지 궁금합니다. 이 메서드는 사용할 수 있는지, 그리고 어떻게 사용하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  13시간 전



    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

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 29,721건 / 3 페이지

검색

게시물 검색