DateTime::createFromImmutable()
The original DateTime
class that represents date and
time information is, and has always been, mutable. This means that
DateTime
objects can be modified through methods such
as DateTime::add()
.
The DateTimeImmutable
class, which was added later,
is, as its name gives away, immutable. Once created, a
DateTimeImmutable
object cannot be modified. This is
great as immutable state should be favored over mutable state.
Ever since its introduction, the DateTimeImmutable
provides the createFromMutable()
method to create a
DateTimeImmutable
object based on an existing
DateTime
object:
$mutable = new DateTime;
$immutable = DateTimeImmutable::createFromMutable($mutable);
The other way around, though, creating a DateTime
object based on an existing DateTimeImmutable
object
was not as easy. This has been addressed with the introduction of
the DateTime::createFromImmutable()
method:
$immutable = new DateTimeImmutable;
$mutable = DateTime::createFromImmutable($immutable);