Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:65929 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22491 invoked from network); 19 Feb 2013 12:00:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Feb 2013 12:00:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=derick@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=derick@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 82.113.146.227 as permitted sender) X-PHP-List-Original-Sender: derick@php.net X-Host-Fingerprint: 82.113.146.227 xdebug.org Linux 2.6 Received: from [82.113.146.227] ([82.113.146.227:56973] helo=xdebug.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/40-17768-56963215 for ; Tue, 19 Feb 2013 07:00:39 -0500 Received: from localhost (xdebug.org [127.0.0.1]) by xdebug.org (Postfix) with ESMTPS id 3744BDE13D; Tue, 19 Feb 2013 12:00:34 +0000 (GMT) Date: Tue, 19 Feb 2013 12:00:34 +0000 (GMT) X-X-Sender: derick@whisky.home.derickrethans.nl To: Nikita Popov cc: PHP internals In-Reply-To: Message-ID: References: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: Re: [PHP-DEV] Questions regarding DateTimeImmutable From: derick@php.net (Derick Rethans) On Fri, 15 Feb 2013, Nikita Popov wrote: > 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. It was mostly done so that methods and code accepting do not need signature changes. In many cases library code would accept a DateTime, call modify and return the changed object - not realizing that the original object was changed as well causes issues with calling code. By having DateTimeImmutable extend DateTime they can continue to pass them in, without having to change library code at all. > 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?) It just returns the changed object instead of changing the object itself *and* returning the changed object (as DateTime does). cheers, Derick