Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81512 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11876 invoked from network); 1 Feb 2015 01:06:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Feb 2015 01:06:55 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.216 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.216 imap10-3.ox.privateemail.com Received: from [192.64.116.216] ([192.64.116.216:38536] helo=imap10-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/39-01632-E2C7DC45 for ; Sat, 31 Jan 2015 20:06:54 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 0D0F72400A9; Sat, 31 Jan 2015 20:06:51 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap10.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap10.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id uSmiEM823L75; Sat, 31 Jan 2015 20:06:50 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 0D0312400AA; Sat, 31 Jan 2015 20:06:49 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: <54CD7975.8040908@gmail.com> Date: Sun, 1 Feb 2015 01:06:48 +0000 Cc: Larry Garfield , internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: References: <54CBC804.7050706@gmail.com> <54CD7668.30301@garfieldtech.com> <54CD7975.8040908@gmail.com> To: Stanislav Malyshev X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: ajf@ajf.me (Andrea Faulds) Hi Stas, > On 1 Feb 2015, at 00:55, Stanislav Malyshev = wrote: >=20 >=20 >> The with*() methods in PSR-7 are documented to return a new instance, >> not modify the existing instance. Yes, there's no way in PHP itself = to >> force that syntactically, which is why documentation exists. :-) >>=20 >> Also, in the benchmarks we've run the performance cost of all those = new >> objects is measured in nanoseconds, ie, small enough that we're not >> worried about it. (Hats off to the PHP Internals folks for making = that >> fast!) >=20 > It is great that this is fast, but I wonder (maybe off-topic?) why do > it? I.e. it is clear that in something like: >=20 > $a =3D new Request->withHeaders(...)->withBody(...) > ->withEncoding(...)->withETag(...) >=20 > the intermediate objects are useless and nobody needs 5 new objects = when > you do it. Am I missing something here? I assume the reason for doing this is so you can=E2=80=99t ever modify = the object from a distance, you must always create a new one to avoid = messing up anything with an existing handle on it. As you mention, though, this means that you get useless intermediate = objects. This use case could be solved much better if we had = copy-on-write/value-type classes like PHP 4 had. If Request was a = value-type class, then you could do this: $a =3D new Request(); $a->addHeaders(=E2=80=A6); $a->setBody(=E2=80=A6); $a->setEncoding(=E2=80=A6); $a->setETag(=E2=80=A6); Here, there=E2=80=99s no redundant objects made, but if you pass $a on, = it=E2=80=99d be automatically copied by PHP, so you don=E2=80=99t need = to worry about it being modified. Would that make sense? It=E2=80=99s no different than how our existing = value types like scalars and arrays work. -- Andrea Faulds http://ajf.me/