On 17 November 2015 at 11:04, Rowan Collins <rowan.coll...@gmail.com> 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. > > 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. > > [1] https://github.com/guzzle/psr7/blob/master/src/Request.php#L101 > > Regards, > -- > Rowan Collins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Could clone take an array parameter of immutable property overrides? $copied = clone ($immutable, ['foo' => 'bar']);