Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66957 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77376 invoked from network); 4 Apr 2013 17:59:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Apr 2013 17:59:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.45 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.219.45 mail-oa0-f45.google.com Received: from [209.85.219.45] ([209.85.219.45:44224] helo=mail-oa0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3C/A8-35962-27FBD515 for ; Thu, 04 Apr 2013 12:59:15 -0500 Received: by mail-oa0-f45.google.com with SMTP id o6so3125850oag.18 for ; Thu, 04 Apr 2013 10:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=0Dts0yqLb0cY/sOdifN1BV0FlfSYuFRYIU/Zy3SjlO0=; b=aNkhFs9i8n+fbRvgVoJ5Mw0Jki1cgmcmips/MHo6rOSixdKY/wO4+fG3EdKn0Uac10 eZbd2nVKj0036vn4089iLif5k6cmRLAQWuPNodD9BTJWpAyJtNPrAVeCeT4OhA6rXgEc hrxZ0+PHiNgHA1tBbKXBbfqFnCUqNEzxMd3YucWZyo1cVeswE7tTktT5V0mKPRd4niH0 NW/wZSfgmCLqDLZBbjWU0QxLqoJM9PlI8lhxAEKNpcZVI6n0Kz0b7SvUWqqed3ZpKZH9 VAEyYK5nc3Frgz73mc/Vl+0qTLK3nSYLlaMHbgPbzMQ3nzZQ1ZEDN++0kq6daLgQIiFz E5nQ== X-Received: by 10.182.118.104 with SMTP id kl8mr5060393obb.54.1365098352321; Thu, 04 Apr 2013 10:59:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.76.9.232 with HTTP; Thu, 4 Apr 2013 10:58:52 -0700 (PDT) In-Reply-To: References: Date: Thu, 4 Apr 2013 19:58:52 +0200 Message-ID: To: Madara Uchiha Cc: Rasmus Schultz , PHP Internals Content-Type: multipart/alternative; boundary=f46d044786b55ffd2c04d98cba86 Subject: Re: [PHP-DEV] a couple of thoughts on the DateTime type debate From: linepogl@gmail.com (Lazare Inepologlou) --f46d044786b55ffd2c04d98cba86 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2013/4/4 Madara Uchiha > OOP is not a beginner's concept. I don't want to sacrifice good coding > practices for a better learning curve. > > This is interesting. Best practices from other languages, including C#, Scala etc, have shown that some things are better represented by value types. Even in PHP, strings are value types, because it is better this way... Besides, having to think whether to use a value type or a reference type does not make anything easier to learn! Value types is an advanced, post-OOP concept, not targeted to beginners anyway. > Also, a glance on the manual would reveal that the method returns the sam= e > 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 =3D 1.0; > > public $g =3D 1.0; > > public $b =3D 1.0; > > } > > > > Stored internally this might be something like: > > > > array('__type'=3D>'Color', 'r'=3D>1.0, 'g'=3D>1.0, 'b'=3D>1.0) > > > > Have you worked extensively with DateTime or other value-as-object type= s? > > > > $today =3D new DateTime(); > > $yesterday =3D $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 sam= e > > 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 lik= e > > this: > > > > public function __construct(DateTime $begin, DateTime $end) > > { > > $this->begin =3D clone $begin; > > $this->end =3D 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 =3D new DateTime(); > > $week =3D array(); > > for ($i=3D0; $i<7; $i++) { > > $week[] =3D 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 thin= k? > > > > 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 oth= er > > 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 myse= lf > > (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... > > > Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel --f46d044786b55ffd2c04d98cba86--