Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81433 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66271 invoked from network); 30 Jan 2015 15:39:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2015 15:39:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=lisachenko.it@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=lisachenko.it@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.172 as permitted sender) X-PHP-List-Original-Sender: lisachenko.it@gmail.com X-Host-Fingerprint: 209.85.212.172 mail-wi0-f172.google.com Received: from [209.85.212.172] ([209.85.212.172:64015] helo=mail-wi0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0E/85-35409-7B5ABC45 for ; Fri, 30 Jan 2015 10:39:36 -0500 Received: by mail-wi0-f172.google.com with SMTP id h11so3906381wiw.5 for ; Fri, 30 Jan 2015 07:39:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=8YHfE8q4OWy/StD54HIqwKSFFE6glw8IM6kLW+gKJxM=; b=iBLrtf6KI1EAKCyeBk/xCj3nvfkqn3d9jZuBMNyFml2155YzthinIUu6yx/Ay3E6Gk D7YxJaQIP5Qx6lxuY8GCPSstTpNwRSKU+y7ijn5os/C3j0TJ69WHe6PxQwt/78m2Skx9 LQUdFeQCmyXG2o8+SLmQzq8rnMREnnlKBW9OPpd3gB5oTiXQhHe9dQdFxjtb7xveSBK7 q7RDHqxjy4jtJKBhFBqOrf7J8xqADJy5Oqqf0OFYXp4scYtJVTQ6+LtnDeWCfOQPIOxP v7zX1yWobCVr7Z6+24AzP5Eq2gT5qX4/mm41Kp7rnwWlg2GUfXzr8jh7jF0tjM+K0vJ+ YOYQ== MIME-Version: 1.0 X-Received: by 10.194.202.129 with SMTP id ki1mr13167088wjc.76.1422632372996; Fri, 30 Jan 2015 07:39:32 -0800 (PST) Received: by 10.194.154.229 with HTTP; Fri, 30 Jan 2015 07:39:32 -0800 (PST) In-Reply-To: <8FD5ABB0-CF46-4E43-9299-5B77E389734F@ajf.me> References: <8FD5ABB0-CF46-4E43-9299-5B77E389734F@ajf.me> Date: Fri, 30 Jan 2015 18:39:32 +0300 Message-ID: To: PHP internals list Content-Type: multipart/alternative; boundary=047d7b676a283d100b050de06911 Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: lisachenko.it@gmail.com (Alexander Lisachenko) --047d7b676a283d100b050de06911 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2015-01-30 18:07 GMT+03:00 Andrea Faulds : > This approach wouldn=E2=80=99t solve the problem you=E2=80=99re describin= g. 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. Thank you for this answer. However, pay an attention to the last part of my suggestion. To create a mutable copy of immutable object it will be possible just to make an assignment (this will create a copy/clone) to a local mutable variable and adjust it for your needs. example: public function handleRequest(const Request $request) { // Mark $request as immutable for body // we can not change $request variable itself // but if need, we can only initialize a copy of it // $request->url =3D 'https://evil.com'; // Exception will be there $mutableSubRequest =3D $request; // copy of immutable object or maybe a direct Copy-on-Write? $mutableSubRequest->uri =3D 'https://example.com'; // It's ok to modify our local copy of an object $this->kernel->handleRequest($mutableSubRequest); } 2015-01-30 18:07 GMT+03:00 Andrea Faulds : > Hi Alexander, > > > On 30 Jan 2015, at 13:07, Alexander Lisachenko > wrote: > > > > Hello, internals! > > > > Today I was looking at PSR-7 and discovered this part of code: > > > > $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); > > > > 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 f= or > > 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 describin= g. 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 classe= s. > > Thanks. > -- > Andrea Faulds > http://ajf.me/ > > > > > --047d7b676a283d100b050de06911--