Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97363 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74433 invoked from network); 12 Dec 2016 05:58:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Dec 2016 05:58:58 -0000 Authentication-Results: pb1.pair.com header.from=me@daveyshafik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@daveyshafik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain daveyshafik.com from 209.85.161.171 cause and error) X-PHP-List-Original-Sender: me@daveyshafik.com X-Host-Fingerprint: 209.85.161.171 mail-yw0-f171.google.com Received: from [209.85.161.171] ([209.85.161.171:34692] helo=mail-yw0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E2/65-27518-0AC3E485 for ; Mon, 12 Dec 2016 00:58:56 -0500 Received: by mail-yw0-f171.google.com with SMTP id t125so57288461ywc.1 for ; Sun, 11 Dec 2016 21:58:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daveyshafik-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=blJv9Wz5vtLxrUFjWNkCUyEvrNv50TIUmr8RfwROg8w=; b=GiJDWnGwiatbQXPGr8rp8jcsFwbXn5Z4mUj9UkwZ2zX0OZd+hVAr5c9pqoaNIlMBzv stqnJsqT6Z0HPQkQ327olR0Ksvs6Dgt9SCbF9c2KpvYeLsC8s/zF1aImaoxPTbWhXmts h2ZX8piiyLIqLEjAHpwdVeNmysgME/cOG5fq1iCmbTMaTVut3bB1nHgVT8CO7ipZ8fMr DxwDZIRL3C2wTRcqzZuuD2nctu+yECcaAkP1pKbCCXEvUyeGcHnL4GBy/GBSmUZ/bWuX 9n0F1ZOjr8Qlp0D4S7BZAGJqO7sZt5MjRYjTmq9fyrKGL/pSSM4+JuVhXBMFlYS9XtI7 EKOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=blJv9Wz5vtLxrUFjWNkCUyEvrNv50TIUmr8RfwROg8w=; b=EI/fOcS7kzKRKml6SVM878defwZTMPVXj6UkUPAaQWwr9/HryuXkem2uVV1vbah5Kd afN1s8DjXv5O6F3sKt2ovjYfOplisofqC6gp7FZJAB+6FJEUblHXLAiLvhi+GWHEd6pT zmilt/9558OG1RBH+rtw2Rni8oPazLDjpXWqmQRxt6INr9GwfWCCZnoz8sjp8uMgls54 piuKqsbWRnLEjEIoxCMZZxL1bULZGx0HfAzcoNcMhzeX7Sp5Nkq9KAHHmPs5yKhcZJ9Q tSgcstwBRL5W7NB1kylsrsJ8n3xuddJTzU8bxn0LY6Up4a3sIoCjfVHrxKROhwzVs4Ux lAKg== X-Gm-Message-State: AKaTC00BSitJFZIb8KJigqNuOwGORFl7FX3bpI/M0F6ha8iXaWYb3QRnqImwLE0CGsequgFQyjsj4Oc+3h/ZVFIq X-Received: by 10.129.51.204 with SMTP id z195mr84998820ywz.352.1481522333282; Sun, 11 Dec 2016 21:58:53 -0800 (PST) MIME-Version: 1.0 Sender: me@daveyshafik.com Received: by 10.129.160.147 with HTTP; Sun, 11 Dec 2016 21:58:52 -0800 (PST) In-Reply-To: <79ea244b-9fe7-5b05-d429-bf5585215542@texthtml.net> References: <1807f949-81d3-f2c4-8706-0fdade3ea51d@garfieldtech.com> <4635ac4b-844a-b023-5ad9-e8524a156404@texthtml.net> <79ea244b-9fe7-5b05-d429-bf5585215542@texthtml.net> Date: Mon, 12 Dec 2016 00:58:52 -0500 X-Google-Sender-Auth: JbsWP5qOy685t_QbPU_Y3UaUsmo Message-ID: To: Mathieu Rochette Cc: =?UTF-8?Q?Silvio_Mariji=C4=87?= , Larry Garfield , PHP Internals List Content-Type: multipart/alternative; boundary=001a1142216e672fc805436fccfa Subject: Re: [PHP-DEV][RFC][DISCUSSION] - Immutable classes and properties From: davey@php.net (Davey Shafik) --001a1142216e672fc805436fccfa Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Sun, Dec 11, 2016 at 7:40 PM, Mathieu Rochette wrote: > > > On 12/12/2016 01:16 AM, Silvio Mariji=C4=87 wrote: > > This have occurred to me also, I wanted to separate that into two RFC's= , > > but then how we deal with RFC's that depend on each other? > well that's just my opinion but I don't think they depend on each other, > this rfc seems useful on its own. The "clone" one might depends on this > one but it does not *need* to either (otoh it certainly would be much > more useful with immutable classes than mutable classes) > > plus, this part might already trigger enough discussion and adding > everything at once might get confusion. I can think of : clone, late > initialization, DateTimeImmutable and probably other subjects that could > make the discussion noisy > > I'm not as familiar as you are with this mailing list so I might very > well be wrong too ^^ > > > > 2016-12-12 1:03 GMT+01:00 Larry Garfield : > > > >> Assuming this was intended for the list... > it was, thank you > >> > >> On 12/11/2016 05:55 PM, Mathieu Rochette wrote: > >> > >>> Currently the only "unlocked context" for an object is its > >>> constructor. As discussed previously, that is insufficient. For any > >>> non-trivial object (more than 1-3 internal properties) creating a new > >>> one via the constructor only when incrementally building is > >>> prohibitively difficult. The pattern of with*() methods that spawn > >>> new objects via clone(), a la PSR-7, needs to be supported. That is: > >>> > >>> immutable class Money { > >>> // ... > >>> > >>> public function add(Money $other) : Money { > >>> $new =3D clone($this); > >>> $new->amount +=3D $other->amount; > >>> return $new; > >>> } > >>> } > >>> > >>> I'm not sure how easily we can denote that sort of case. It's outsid= e > >>> the __clone() function itself, which is what makes it difficult. > >>> Without that, though, such immutable objects are of only limited use. > >>> > >> > >> > >>> As you said, it has been already been discussed that a method to buil= d > >>> new altered object from an existing one could be improved. Different > >>> options were proposed but maybe it's better to start small to get thi= s > >>> first part right and add this in another rfc ? having everything at t= he > >>> same time might makes the rfc more difficult to be accepted > >>> > >> On the contrary, an RFC that doesn't fully solve the issue at hand and > >> leaves major gaps in place is a poor RFC, and I would expect to be > >> justifiably voted down. > I wonder if we can essentially make all public methods act like they are static (no access to $this) and then use something like: immutable class Foo { protected $bar; public function getBar() with (Foo $instance): string // $instance is a clone of $this { // context =3D=3D $instance, so access to private/protected return $instance->bar; } public function withBar($bar) with (Foo $instance): Foo { $instance->bar =3D $bar; // Allowed as we're in scope return $instance; } } Only operations in the scope of the class itself (and it's descendants, adhering to standard visibility rules) can make changes on the cloned variant. The `with ($instance)` is optional, $instance is just a variable name. In reality you'll want to use it most of the time I imagine. I suppose you can re-use the `use` keyword here too. Also, this shouldn't allow for immutable properties. Another option is to look at the way that HHVM does immutable types, though that is _file_ based. - Davey --001a1142216e672fc805436fccfa--