Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84476 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89436 invoked from network); 9 Mar 2015 14:18:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2015 14:18:05 -0000 Authentication-Results: pb1.pair.com header.from=leverton@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=leverton@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.177 as permitted sender) X-PHP-List-Original-Sender: leverton@gmail.com X-Host-Fingerprint: 209.85.192.177 mail-pd0-f177.google.com Received: from [209.85.192.177] ([209.85.192.177:44332] helo=mail-pd0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/11-18276-B9BADF45 for ; Mon, 09 Mar 2015 09:18:04 -0500 Received: by pdjz10 with SMTP id z10so44280754pdj.11 for ; Mon, 09 Mar 2015 07:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=ACKWfwBCdSAu1NwxKOzMPkbvfHNTip4stf0Dl2WS0g8=; b=z5ZNH3MdmxkIxvfvNO23VrTr433si6jPcXfMu0sF68rEpxpv5/jgyeBblr8dqk6A6w XjUxMD6XLftrZk/y1d9kZhusFhJSaKvy0/dHcQYd7PBQMSQsH9VnmPd32n4pXi8IGkCW awQ9V8kVDKpHIpUJxRwE3MAn6N9yEVOrCq5e42lZOqt6bqZnm2YnMToYc3AW0sqpVhxV UB5j0JH2nRzGSyZ2+CJaXUjDc9C/yb6yVNZbAol21qurzHB711FE4CusaZWwHA6Ci1Ey nfcMP1Gm1IpDtul3mxObsU4g/r24XdIQll5G+oahmQuj5xsJKCpTXxf6Phhz3HokBsFh 1vbg== MIME-Version: 1.0 X-Received: by 10.70.128.15 with SMTP id nk15mr53154934pdb.121.1425910678861; Mon, 09 Mar 2015 07:17:58 -0700 (PDT) Received: by 10.70.65.130 with HTTP; Mon, 9 Mar 2015 07:17:58 -0700 (PDT) In-Reply-To: References: Date: Mon, 9 Mar 2015 09:17:58 -0500 Message-ID: To: Derick Rethans Cc: Trevor Suarez , PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [PR] DateTime::createFromImmutable() method From: leverton@gmail.com (Matthew Leverton) On Mon, Mar 9, 2015 at 5:35 AM, Derick Rethans wrote: > On Thu, 5 Mar 2015, Trevor Suarez wrote: > >> Good morning internals! >> >> I would like to propose a small addition be made to the DateTime class. >> >> https://github.com/php/php-src/pull/1145 > > When the factory method was added, we had this same discussion. And the > discussions lead to the conclusion that it does not make sense to have > this "createFromImmutable" factory method on DateTime. Basically with > the same reasons that people should type hint against the Interface > instead. > If you have a method that needs to modify the DateTime, then you must choose between DateTime or DateTimeImmutable. (The interface is read only.) That is, I think it is poor practice to repeat the following over and over in every method: function changeTheDate(DateTimeInterface $dt) { $immutable = new DateTimeImmutable($dt->format('c')); $immutable->setTimeZone(...); // etc return $someCalculation; } That function really needs a DateTimeImmutable, so the burden of converting it ought to be on the caller. It might be able to optimize the number of conversions, etc. I almost always use DateTimeImmutables ... fewer side effects, etc. However, many libraries and code I interface with require a DateTime for such things. (Yes, perhaps they just incorrectly type hint for a DateTime, but I cannot help that.) So I need to convert from DateTimeImmutable to DateTime. e.g., function foo(DateTime $dt) { } $dt = new DateTimeImmutable; foo(new DateTime($dt->format('c')); // the method I use foo(new DateTime($dt)); // doesn't work foo(DateTime::fromInstance($dt)); // doesn't exist (It's possible that DateTimeImmutable::fromInstance($dateTimeImmutable) could just return the same object.) So while not a pressing issue, I do think a more straightforward way of converting would be good because the reality is that you're going to need to do the conversions at some point if you interface with other people's code, no matter how hard you try to use good practices. -- Matthew Leverton