Hi internals,

for PHP 5.5 a new DateTimeImmutable class was introduced, which is a
variant of DateTime, which has methods working on a clone of the original
object.

There was no RFC on this and some of the design decisions are a bit
uncleared to me, so I figured I'd write a mail:

a) The DateTimeImmutable class extends the DateTime class. Why was this
done this way? Both classes are incompatible (as in the inheritance
violates LSP). E.g. if you hint against DateTime, then passing in
DateTimeImmutable will not work (as the behavior is different). The same
also applies the other way around (DateTime extending DateTimeImmutable).
Imho a mutable and an immutable variant of a class can't reasonably extend
from one another, so I'm not sure why this was done.

b) The DateTimeImmutable class is (method-wise) an exact copy of DateTime.
This makes for some rather weird method names for an immutable object. E.g.
there is DateTimeImmutable::modify, which to me seems quite contradictory.
There are also a lot of methods of the DateTimeImmutable::setTime form,
which also seems rather odd and confusing (how can I set something on an
immutable object?)

The DateTimeImmutable class was added to fix design issues with DateTime,
but the way it was implemented I think it just replaced one evil with
another one. Could we maybe reconsider this?

Nikita

Reply via email to