Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66951 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66453 invoked from network); 4 Apr 2013 16:46:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Apr 2013 16:46:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.128.171 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.128.171 mail-ve0-f171.google.com Received: from [209.85.128.171] ([209.85.128.171:36933] helo=mail-ve0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C7/66-35962-16EAD515 for ; Thu, 04 Apr 2013 11:46:26 -0500 Received: by mail-ve0-f171.google.com with SMTP id b10so2866326vea.30 for ; Thu, 04 Apr 2013 09:46:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:x-gm-message-state; bh=VP7kMm2BC8c9+Cmv/Q8+cFvv5638zLA6jHifijU3r9g=; b=gs3eU5+LpWEqBtv/7k4f3GpGIT+OdpD6R4KGUwjoQvtCLrcIIKe2cRc6d4EJ1hC5Gp Yhu9u5RRELl7TRUKZ+Z9lIwbyaqA1xz6aMP3cVWEL46nV3Sf5Jx0xNzZcnezNR8MrF8+ Gfp7IzGc+fSgrfJIZUnftWU8/f7l6JAO56G5eowgjCjgHNJ8v2Zx+X8KPEuXVImkKbNS F5BFeGinf/RJWaIZgrEN/6lpFYWFxETUBOm5YJFAlDmqaWW2gyHO+OYWhxp0Nz+GrrVh MgXCDsezTp5LZ6xeqWN7o18lv5lpUO9zMRAVPkmIJQwjhYDaT6goyY7lhn5C4hSsGBDu jUdw== MIME-Version: 1.0 X-Received: by 10.52.72.40 with SMTP id a8mr4643052vdv.20.1365093983038; Thu, 04 Apr 2013 09:46:23 -0700 (PDT) Received: by 10.58.208.234 with HTTP; Thu, 4 Apr 2013 09:46:22 -0700 (PDT) In-Reply-To: References: Date: Thu, 4 Apr 2013 12:46:22 -0400 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary=20cf307f360cf2121e04d98bb5d8 X-Gm-Message-State: ALoCoQnpERDRozcx6XrWWc9Liy5G9ib+sOK9PsfKJXqT3aP9HVw+DCHKmGwrMp6IRkArrN/6YRMp Subject: Re: [PHP-DEV] a couple of thoughts on the DateTime type debate From: rasmus@mindplay.dk (Rasmus Schultz) --20cf307f360cf2121e04d98bb5d8 Content-Type: text/plain; charset=ISO-8859-1 Is it a really big feature if it's just syntactic sugar and internally stored as an array? say: struct Color { public $r = 1.0; public $g = 1.0; public $b = 1.0; } Stored internally this might be something like: array('__type'=>'Color', 'r'=>1.0, 'g'=>1.0, 'b'=>1.0) Have you worked extensively with DateTime or other value-as-object types? $today = new DateTime(); $yesterday = $today->modify('-1 day'); echo "today: " . $today->format('Y-m-d') . "\n"; echo "yesterday: " . $yesterday->format('Y-m-d'); Code like this is a major mind-boggle to most programmers - it's not by any means obvious from reading this code that $yesterday and $today are in fact references to the same object, and the output of this script is the same date, twice. Most programmers automatically think of "atomic" things like date/time and color as being values, and it's easy (even for experienced developers) to make mistakes. In my own code, for example, constructors that take DateTime instances as arguments usually have to safeguard by doing something like this: public function __construct(DateTime $begin, DateTime $end) { $this->begin = clone $begin; $this->end = clone $end; } This makes redundant copies of objects in every instance, which isn't optimal - but enables code like this to actually do what you expect: $date = new DateTime(); $week = array(); for ($i=0; $i<7; $i++) { $week[] = new Day($date); $date->add('P1D'); } if the Day constructor doesn't clone $date, you have problems - that's what started the debate about a new DateTime type in the first place, I think? existing DateTime is fine, if what you want is a timestamp you can manipulate for reasons *other* than using the resulting value, e.g. for printing out successive dates - as soon as you want to do something other than using the immediate value contained in the object, trouble's a-brewin'. a new DateTime-type would solve that by having the add() and sub() methods clone internally - but I can already do that by extending DateTime myself (as countless frameworks and libraries have done) with all the same problems in terms of API issues and expected behavior. (in cases where you do expect the internal value to change.) as said, it only addresses the problem for DateTime, which is just one isolated example of natural value-types represented as dysfunctional objects. why not address the real issue, which is the lack of value-types? other languages have them for the same reasons. had we had value-types at the time, no one would have thought to implement DateTime as an object... --20cf307f360cf2121e04d98bb5d8--