Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tentative Static Returntype With Datetime

The createFromImmutable() method from DateTime and DateTimeImmutable always return an object of the same class. In PHP 8.2 and later, the return type is now static, it will tentatively return a children class, when the method is called from that child class.

PHP code

<?php

class A extends DateTime{}

$date = new DateTimeImmutable("2014-06-20 11:45 Europe/London");

$mutable = A::createFromImmutable( $date );

var_dump($mutable);
?>

Before


After

object(A)#2 (3) {
  ["date"]=>
  string(26) "2014-06-20 11:45:00.000000" 
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London" 
}

PHP version change

This behavior changed in 8.2.

Extension

© 2023-2026, Damien Seguy - Exakat