Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89267 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97997 invoked from network); 17 Nov 2015 13:41:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Nov 2015 13:41:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=mathieu@rochette.cc; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mathieu@rochette.cc; sender-id=pass Received-SPF: pass (pb1.pair.com: domain rochette.cc designates 195.154.14.121 as permitted sender) X-PHP-List-Original-Sender: mathieu@rochette.cc X-Host-Fingerprint: 195.154.14.121 texthtml.net Received: from [195.154.14.121] ([195.154.14.121:39707] helo=texthtml.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/20-30130-D6E2B465 for ; Tue, 17 Nov 2015 08:41:02 -0500 Received: by texthtml.net (Postfix, from userid 99) id AEF45101F81; Tue, 17 Nov 2015 14:31:12 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on texthtml.net X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from [192.168.1.130] (localhost [127.0.0.1]) by texthtml.net (Postfix) with ESMTPA id E1627101F80; Tue, 17 Nov 2015 14:31:10 +0100 (CET) To: Lester Caine , internals@lists.php.net References: <564A547C.9060504@garfieldtech.com> <564A629C.9040709@rochette.cc> <564A69C1.5080208@garfieldtech.com> <2D.66.34372.2A78A465@pb1.pair.com> <564B09A4.3050706@gmail.com> <564B1D0B.1040003@lsces.co.uk> Message-ID: <564B2E66.60601@rochette.cc> Date: Tue, 17 Nov 2015 14:40:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <564B1D0B.1040003@lsces.co.uk> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Immutable modifier From: mathieu@rochette.cc (Mathieu Rochette) On 17/11/2015 13:26, Lester Caine wrote: > On 17/11/15 11:04, Rowan Collins wrote: >> If you look at the PSR-7 implementation I linked to earlier [1], there's >> no such boilerplate, just a single call to "clone $this", and PHP does >> it all for you. But that doesn't work with a simplistic definition of >> immutable like "mutable in constructor" or "mutable until non-null"; you >> need to be able to "freeze" an instance once you've set it up, or have >> privileged (private) methods which are allowed to mutate the properties. Indeed, maybe there is a use for a new language construct to help reduce the boilerplate, but it could be done afterward, the "everything in the constructor" works fine. If there is too much in the construct it might be a sign that the class is doing too much and should be refactored in smaller classes slightly off topic, in rust you initialize struct like that : MyStruct { myprop1: myvalue1, myprop2: myvalue2, } It could be extended in php to have something like that : class MyClass { private $myProp1; private $myProp2; static create function ($prop1) { return new self { myProp1: $prop1; myProp2: 'initialValue' }; } public function withProp2($prop2) { return new self { myProp2: $prop2, *: $this, // here * would mean: properties other than myProp2 are taken from $this }; // or like that return self { myProp2: $prop2, ... $this, // properties other than myProp2 are taken from $this }; } } note: I'd like this kind of construct even without the immutable stuff (would avoid having to write boilerplate __construct) > read_only=true; ... after the value is set. > > Switch off all the 'setter' magic once the object has been created, but > I still see a problem where this only really works with simple values? > If I've created a more complex object, while it's 'value' may need to be > fixed, how it's displayed may involve setting alternative defaults, so > one still needs to be able to set other variables in the object. In that case if you don't want to make its "display settings" immutable you should have 2 classes, one with the immutable 'value' and another containing the 'value' and the "display settings" >