Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97374 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27833 invoked from network); 12 Dec 2016 18:06:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Dec 2016 18:06:15 -0000 Authentication-Results: pb1.pair.com header.from=marijic.silvio@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=marijic.silvio@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.178 as permitted sender) X-PHP-List-Original-Sender: marijic.silvio@gmail.com X-Host-Fingerprint: 209.85.223.178 mail-io0-f178.google.com Received: from [209.85.223.178] ([209.85.223.178:34257] helo=mail-io0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F5/7D-27518-517EE485 for ; Mon, 12 Dec 2016 13:06:14 -0500 Received: by mail-io0-f178.google.com with SMTP id p42so11630025ioo.1 for ; Mon, 12 Dec 2016 10:06:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=xQhNBjybRMFKSAE7vUsFZeMM4tJg5bp9fwm4/nWPjhI=; b=Dc2WWHGmkMNumtjsuoTjZmtS7wHe0KodeMMxC4mvMk9c1a+jWe6KnwRnx5SvMWOcQ9 e6R+HgfvcR6QmR+PKcFUt62xEPMWWNdZBfuLi2s3NwlBi9oh7txjNlX0M0U1pH6azmEX uIuJ+UsnCzk7JTiu1XiIbqsi3lwHOjNm+EGdwgRJppJx48hF4LT0V3M4IE6izFXfAsDJ e7CO6q5sU+JeQWCwVqTliaSgz5i8m7nQOVc4QB2tsQ3SLESPjQF0NIMTXxLk9hsG/HiL Bo2moIvUuTtl+bqGpIObtjG7TtsKGC7z5T4V1SEi1D8GlnOlUTgauiLQyGBSiB/fVLv4 mexg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=xQhNBjybRMFKSAE7vUsFZeMM4tJg5bp9fwm4/nWPjhI=; b=PaFcStb/sl5yg4UEQUXoeBRnqZbD0x1CGz8a1jDOoA/1Ojl+T1aVnzHuVo7cH1FFqD 6bMRFs90IH1h6L32VpbAT2zzFiY8KM493eoVDZ7bu8+gbTsSlp3MwINYXX3qN0Fp0hzF gw/HzNAMUPwvfFfRh2p+CgEfLPS/uqShfvt/9M1/TeiW1+RM/s/3dz246YnjYWrYhgG9 VB61CeOcz5k0VkgP0Rl1482ZJUyN3TMYCGZQ4iEqvhVTcAz28QjLxG+dLNn1/5poR1iV ZW/emRREKzIvTSD72azOcZIFb1Q7pVespOwMAl2WOtBpm+yvIqhX6Z5NR7wr+jTMPCVj BD4Q== X-Gm-Message-State: AKaTC03lpzlWIKEBTjXq/f9H3h3TwHSndCHQYn5jTfgK94g1QU+itTPwZCiaN1xshQWDBubN4yIAcgjQ6IJdfA== X-Received: by 10.107.183.148 with SMTP id h142mr78667507iof.190.1481565970433; Mon, 12 Dec 2016 10:06:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.36.110.142 with HTTP; Mon, 12 Dec 2016 10:06:09 -0800 (PST) In-Reply-To: References: <1807f949-81d3-f2c4-8706-0fdade3ea51d@garfieldtech.com> <4635ac4b-844a-b023-5ad9-e8524a156404@texthtml.net> <79ea244b-9fe7-5b05-d429-bf5585215542@texthtml.net> <76C07EDB-56B3-46AB-A5C1-92485B32C113@gmail.com> Date: Mon, 12 Dec 2016 19:06:09 +0100 Message-ID: To: Rowan Collins Cc: PHP Internals List Content-Type: multipart/alternative; boundary=94eb2c0b8e6e610c37054379f5a2 Subject: Re: [PHP-DEV][RFC][DISCUSSION] - Immutable classes and properties From: marijic.silvio@gmail.com (=?UTF-8?Q?Silvio_Mariji=C4=87?=) --94eb2c0b8e6e610c37054379f5a2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, this would be basic implementation of 'modify' function which would operate on cloned object PHP_FUNCTION(modify) { zval *obj, *properties, *entry; zend_long h; zend_string *key; if (zend_parse_parameters(ZEND_NUM_ARGS(), "oa", &obj, &properties) =3D=3D FAILURE) { return; } ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(properties), h, key, entry) { zval member; ZVAL_STR(&member, key); zend_std_write_property(obj, &member, entry, NULL); zval_ptr_dtor(&member); } ZEND_HASH_FOREACH_END(); RETVAL_OBJ(Z_OBJ_P(obj)); } immutable class User{ public $firstname; public $lastname; public function __construct($firstname, $lastname){} public function withFirstName($firstname){ return modify($this, ['firstname' =3D> $firstname]); } } $user =3D new User('John', 'Doe'); $new =3D $user->withFirstName('Foo'); Any feedback? 2016-12-12 15:43 GMT+01:00 Silvio Mariji=C4=87 : > > > 2016-12-12 15:14 GMT+01:00 Rowan Collins : > >> On 12 December 2016 09:11:17 GMT+00:00, "Silvio Mariji=C4=87" < >> marijic.silvio@gmail.com> wrote: >> >Regarding my proposal above, since copy() would essentially operate >> >only on >> >immutable objects, why not put it only there. So you would be able to >> >make >> >a call like this one $immutable->copy(array $args); or >> >$this->copy(array >> >$args); depending on whether you are inside let's say with* method or >> >outside of the object. >> >> If users of the object can set arbitrary (public) properties this way, >> then you've thrown away a bunch of the free encapsulation the immutable >> keyword gave you in the first place. What you get is an enforced >> copy-on-write, where you can set the properties to whatever state you li= ke, >> just not using normal object passing semantics. That's very different fr= om >> what the RFC shows, which are properties which are publicly readable, bu= t >> whose state is exclusively controlled by the class (and its descendants)= . >> >> >> PS The rule on this mailing list is too always reply *below* the text >> you're replying to. > > > >> I understand that, but I think we'll have to meet in the middle. How >> would you handle situation where for one little change you have to manua= lly >> recreate not so trivial object with a lot of arguments ? I don't think t= hat >> should be a deal breaker for this RFC but I do agree that we should try = to >> find solution to bridge that gap and put that aside in a other rfc. >> > -- >> Rowan Collins >> [IMSoP] >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > > -- > Silvio Mariji=C4=87 > Software Engineer > 2e Systems > --=20 Silvio Mariji=C4=87 Software Engineer 2e Systems --94eb2c0b8e6e610c37054379f5a2--