Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89268 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5202 invoked from network); 17 Nov 2015 15:37:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Nov 2015 15:37:14 -0000 X-Host-Fingerprint: 178.62.40.5 ajf.me Received: from [178.62.40.5] ([178.62.40.5:6022] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/01-30130-9A94B465 for ; Tue, 17 Nov 2015 10:37:13 -0500 Message-ID: <60.01.30130.9A94B465@pb1.pair.com> To: 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> Date: Tue, 17 Nov 2015 15:37:21 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:42.0) Gecko/20100101 Firefox/42.0 SeaMonkey/2.39 MIME-Version: 1.0 In-Reply-To: <564B09A4.3050706@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 178.62.40.5 Subject: Re: [PHP-DEV] Immutable modifier From: ajf@ajf.me (Andrea Faulds) Hi, Rowan Collins wrote: > Hi Andrea, > > Andrea Faulds wrote on 17/11/2015 01:47: >> Larry Garfield wrote: >>> The "everything in the constructor" is the problem. That results in, >>> essentially, an obscenely long function call that just happens to be >>> named __construct(). If I wanted something that obscure and hard to >>> work with I'd just use anonymous arrays. :-) >> >> Huh? >> >> "with" methods and __construct are not exclusive. In fact, you could >> have a private constructor and only have "with" methods. Only allowing >> assignment to properties in the constructor doesn't prevent having a >> nice API: have your methods call the constructor. >> >> I don't see what you're getting at here. >> > > Making the "with" methods wrap the constructor tidies the public API, > but that (private) constructor needs to be just as complex as Larry said > earlier - you've got to manually extract all the properties of one > instance, and pass them as parameters to the other. Or, you could copy > them one by one inside the body of the constructor, which comes to the > same thing - lots of boilerplate. Does it? You can write a five-line constructor which does what you need: private function __construct(array $properties) { foreach ($properties as $name => $value) { $this->{$name} = $value; } } You can do changes like so: public function withName(string $name): self { return new self(array_merge((array)$this, [ "name" => $name ])); } Not much code, no? Thanks. -- Andrea Faulds http://ajf.me/