Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81432 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62257 invoked from network); 30 Jan 2015 15:07:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2015 15:07:37 -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.208 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.208 imap2-3.ox.privateemail.com Received: from [192.64.116.208] ([192.64.116.208:44744] helo=imap2-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 99/05-35409-63E9BC45 for ; Fri, 30 Jan 2015 10:07:37 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id A943A8C007D; Fri, 30 Jan 2015 10:07:31 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap2.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap2.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id XoKwfOJGlju2; Fri, 30 Jan 2015 10:07:31 -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 E5A468C0069; Fri, 30 Jan 2015 10:07:30 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: Date: Fri, 30 Jan 2015 15:07:28 +0000 Cc: PHP internals list Content-Transfer-Encoding: quoted-printable Message-ID: <8FD5ABB0-CF46-4E43-9299-5B77E389734F@ajf.me> References: To: Alexander Lisachenko X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: ajf@ajf.me (Andrea Faulds) Hi Alexander, > On 30 Jan 2015, at 13:07, Alexander Lisachenko = wrote: >=20 > Hello, internals! >=20 > Today I was looking at PSR-7 and discovered this part of code: >=20 > $body =3D new StringStream(json_encode(['tasks' =3D> [ > 'Code', > 'Coffee', > ]]));; > $request =3D $baseRequest > ->withUri($uri->withPath('/tasks/user/' . $userId)) > ->withMethod('POST') > ->withHeader('Content-Type' =3D> 'application/json') > ->withBody($body); > $response =3D $client->send($request); >=20 > What is wrong here? Emulated immutability. All methods will create a > separate instance of request, so > $baseRequest->withUri()->withMethod()->withHeader()->withBody() will = create > total 5 object instances. It's a memory overhead and time consumption = for > each method call. Yes, I also think this is unfortunate. > What I want to discuss is true immutability flag for variables and > parameters. There are a lot of languages that use "final" or "const" > keywords to prevent modification of variables. We can use this = approach by > extending language syntax as following: This approach wouldn=E2=80=99t solve the problem you=E2=80=99re = describing. You *still* need to produce a new request object, because = the request object is immutable. The mutability of its *properties* = isn=E2=80=99t the issue. If you want to avoid creating five different objects, you=E2=80=99d need = to implement value-type objects that are passed by value and use = copy-on-write. Basically, you=E2=80=99d need to re-add PHP 4 style = classes. Thanks. -- Andrea Faulds http://ajf.me/