Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66953 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69903 invoked from network); 4 Apr 2013 17:03:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Apr 2013 17:03:29 -0000 Authentication-Results: pb1.pair.com header.from=dor.tchizik@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dor.tchizik@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.50 as permitted sender) X-PHP-List-Original-Sender: dor.tchizik@gmail.com X-Host-Fingerprint: 209.85.219.50 mail-oa0-f50.google.com Received: from [209.85.219.50] ([209.85.219.50:58072] helo=mail-oa0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/27-35962-F52BD515 for ; Thu, 04 Apr 2013 12:03:29 -0500 Received: by mail-oa0-f50.google.com with SMTP id n1so3061621oag.9 for ; Thu, 04 Apr 2013 10:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=6eG46t7dtkmXokSFKH0k41oranEIHqnlVhYtPAAA6RY=; b=GKzNhNgHxpM1yQ9OBTRqa73MhQhWurf8h0nvN+pIzggH9+8FkVDbwiGYdrrjasi4e4 F6udwVVmTFagAJkF0rwTlr9j6ah1U08lGGasqGMiGn9vBkdKYcZbqm7L0SCrl+YKy7iF zxFQu9kg0iYHee3GxTIDuj7vgMA/o2BBq8LqnRo+EMhZf87GM8A6Xae9tT1j4bLsm777 so2ctHYaPlpv7+oQQTyLdRJrURYwC7jKtDLGPzyiOykVObeALwhJUR2ErSKbr20BkBRT 38RPu/0GKsOo6MKwagolG/OiiMp1QuKxWupWxqJ8BHWpS6/IAsB5PmunoUDXgrGXizPt 3kOg== MIME-Version: 1.0 X-Received: by 10.60.135.131 with SMTP id ps3mr3592123oeb.131.1365095004969; Thu, 04 Apr 2013 10:03:24 -0700 (PDT) Received: by 10.182.128.69 with HTTP; Thu, 4 Apr 2013 10:03:24 -0700 (PDT) Received: by 10.182.128.69 with HTTP; Thu, 4 Apr 2013 10:03:24 -0700 (PDT) In-Reply-To: References: Date: Thu, 4 Apr 2013 20:03:24 +0300 Message-ID: To: Rasmus Schultz Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7b33cde8db9ff504d98bf284 Subject: Re: [PHP-DEV] a couple of thoughts on the DateTime type debate From: dor.tchizik@gmail.com (Madara Uchiha) --047d7b33cde8db9ff504d98bf284 Content-Type: text/plain; charset=ISO-8859-1 OOP is not a beginner's concept. I don't want to sacrifice good coding practices for a better learning curve. Also, a glance on the manual would reveal that the method returns the same instance for chaining (which is also debatable, why do we even do that?) On Apr 4, 2013 7:46 PM, "Rasmus Schultz" wrote: > 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... > --047d7b33cde8db9ff504d98bf284--