Article View: php.internals
Article #1Re: [PHP-DEV] [RFC] Clone with v2
From: "Rob Landers"
Date: Thu, 15 May 2025 12:14
Date: Thu, 15 May 2025 12:14
158 lines
14235 bytes
14235 bytes
--a1c3b7ef47ff49c7835b54d079446da8 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, May 15, 2025, at 13:56, Stephen Reay wrote: >=20 >=20 >=20 > > On 15 May 2025, at 16:44, Andreas Hennings <andreas@dqxtech.net> wro= te: > >=20 > > =EF=BB=BFOn Thu, 15 May 2025 at 08:24, Stephen Reay <php-lists@koale= phant.com> wrote: > > [..] > >>=20 > >>=20 > >> I may be missing something here.. > >>=20 > >> So far the issues are "how do we deal with a parameter for the actu= al object, vs new properties to apply", "should __clone be called befor= e or after the changes" and "this won't allow regular readonly propertie= s to be modified". > >>=20 > >> Isn't the previous suggestion of passing the new property arguments= directly to the __clone method the obvious solution to all three proble= ms? > >=20 > > What exactly should happen then? > > Would the __clone() method be responsible for assigning those proper= ties? > > Or does the __clone() method get the chance to alter the values befo= re > > they are assigned? > > (this would mean they have to be passed by reference) > > I think this last option is the best, because the values in the array > > can be changed without any readonly constraints. > >=20 > > Another option I was thinking of would be to call __clone() after the > > changes are applied, and pass both the original object and the array > > of changes as first parameter. > > But I think this is a dead end. > >=20 > > -- Andreas > >=20 > >>=20 > >> There's no potential for a conflicting property name, the developer= can use the new property values in the order they see fit relative to t= he logic in the __clone call, and it's inherently in scope to write to a= ny (unlocked during __clone) readonly properties. > >>=20 > >>=20 > >>=20 > >> Cheers > >>=20 > >> Stephen > >>=20 > >>=20 > >>=20 > >=20 >=20 > I would suggest that the __clone method should be directly responsible= for making any changes, just as it is now when it comes to deep cloning= or resetting values. >=20 > Yes I realise it means developers need to then opt in and provide the = functionality to support `clone $foo with(bar: "baz")` or whatever synta= x is used.=20 >=20 > If the properties are public properties, there's nothing stopping some= one writing their own clone_with() in userland now; If someone is using= readonly properties I'd suggest they want to specifically manage update= s to those properties themselves anyway.=20 >=20 > Additionally it means "clone with" would be usable for non-public prop= erties at the discretion of the developer writing their code.=20 >=20 > The mental model is also very clear with this: copy the object in memo= ry, and then call __clone(), with the arguments passed to the clone acti= on - which may be none in the case of code that doesn't accept any clone= arguments. The only change from the current model is that it *may* be = passing arguments.=20 >=20 > Cheers >=20 > Stephen=20 >=20 When I was working on Records, one of the important things was the abili= ty to "hook into" the update that was about to happen. For example, if y= ou have a Money type, you'd want to be able to ensure it cannot be negat= ive when updating via `with()`. This is super important for ensuring con= straints are met during the clone. =E2=80=94 Rob --a1c3b7ef47ff49c7835b54d079446da8 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <!DOCTYPE html><html><head><title></title></head><body><div>On Thu, May = 15, 2025, at 13:56, Stephen Reay wrote:</div><blockquote type=3D"cite" i= d=3D"qt" style=3D""><div><br></div><div><br></div><div><br></div><div>&g= t; On 15 May 2025, at 16:44, Andreas Hennings <<a href=3D"mailto:andr= eas@dqxtech.net">andreas@dqxtech.net</a>> wrote:</div><div>> = </div><div>> =EF=BB=BFOn Thu, 15 May 2025 at 08:24, Stephen Reay <= <a href=3D"mailto:php-lists@koalephant.com">php-lists@koalephant.com</a>= > wrote:</div><div>> [..]</div><div>>> </div><div>>&= gt; </div><div>>> I may be missing something here..</div><div= >>> </div><div>>> So far the issues are "how do we deal= with a parameter for the actual object, vs new properties to apply",&nb= sp; "should __clone be called before or after the changes" and "this won= 't allow regular readonly properties to be modified".</div><div>>>= </div><div>>> Isn't the previous suggestion of passing the n= ew property arguments directly to the __clone method the obvious solutio= n to all three problems?</div><div>> </div><div>> What exactl= y should happen then?</div><div>> Would the __clone() method be respo= nsible for assigning those properties?</div><div>> Or does the __clon= e() method get the chance to alter the values before</div><div>> they= are assigned?</div><div>> (this would mean they have to be passed by= reference)</div><div>> I think this last option is the best, because= the values in the array</div><div>> can be changed without any reado= nly constraints.</div><div>> </div><div>> Another option I wa= s thinking of would be to call __clone() after the</div><div>> change= s are applied, and pass both the original object and the array</div><div= >> of changes as first parameter.</div><div>> But I think this is = a dead end.</div><div>> </div><div>> -- Andreas</div><div>>= ; </div><div>>> </div><div>>> There's no potential= for a conflicting property name, the developer can use the new property= values in the order they see fit relative to the logic in the __clone c= all, and it's inherently in scope to write to any (unlocked during __clo= ne) readonly properties.</div><div>>> </div><div>>>&nbs= p;</div><div>>> </div><div>>> Cheers</div><div>>>= </div><div>>> Stephen</div><div>>> </div><div>>= ;> </div><div>>> </div><div>> </div><div><br>= </div><div>I would suggest that the __clone method should be directly re= sponsible for making any changes, just as it is now when it comes to dee= p cloning or resetting values.</div><div><br></div><div>Yes I realise it= means developers need to then opt in and provide the functionality to s= upport `clone $foo with(bar: "baz")` or whatever syntax is used. </= div><div><br></div><div>If the properties are public properties, there's= nothing stopping someone writing their own clone_with() in userland now= ; If someone is using readonly properties I'd suggest they want to= specifically manage updates to those properties themselves anyway. = ;</div><div><br></div><div>Additionally it means "clone with" would be u= sable for non-public properties at the discretion of the developer writi= ng their code. </div><div><br></div><div>The mental model is also v= ery clear with this: copy the object in memory, and then call __clone(),= with the arguments passed to the clone action - which may be none in th= e case of code that doesn't accept any clone arguments. The only c= hange from the current model is that it *may* be passing arguments. = ;</div><div><br></div><div>Cheers</div><div><br></div><div>Stephen = </div><div><br></div></blockquote><div><br></div><div>When I was working= on Records, one of the important things was the ability to "hook into" = the update that was about to happen. For example, if you have a Money ty= pe, you'd want to be able to ensure it cannot be negative when updating = via `with()`. This is super important for ensuring constraints are met d= uring the clone.</div><div><br></div><div id=3D"sig121229152">=E2=80=94 = Rob</div></body></html> --a1c3b7ef47ff49c7835b54d079446da8--
Technical Details
Message-ID:
<74fde04e-1cbf-423c-8b50-7d7eb156a056@app.fastmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!apf9.newsdeef.eu!news.php.netReferences:
<CAB-BujZLV=dccE6WjBFaLMSM977=fmJOirHSTbhJ1GuZj6iPdQ@mail.gmail.com> <266FA35A-15B0-435E-BBFE-1C6926EB0B7E@koalephant.com>Show Full Headers
Complete Article Headers
Message-ID: <74fde04e-1cbf-423c-8b50-7d7eb156a056@app.fastmail.com>
Subject: Re: [PHP-DEV] [RFC] Clone with v2
Date: Thu, 15 May 2025 14:14:48 +0200
From: rob@bottled.codes ("Rob Landers")
References: <CAB-BujZLV=dccE6WjBFaLMSM977=fmJOirHSTbhJ1GuZj6iPdQ@mail.gmail.com> <266FA35A-15B0-435E-BBFE-1C6926EB0B7E@koalephant.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!apf9.newsdeef.eu!news.php.net
Newsgroups: php.internals
X-Original-To: internals@lists.php.net
Delivered-To: internals@lists.php.net
Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5])
by lists.php.net (Postfix) with ESMTPS id 09A481A00BC
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail;
t=1747311182; bh=I0VxIHwl/G89+YBIsXzXZuV0lfw4qPqdzX0YZ/XV2Zw=;
h=Date:From:To:Cc:In-Reply-To:References:Subject:From;
b=O+o6n6pk5k4oP2/kaxn0E7zJV1Zv+HeeDEjwrpTSHSL5ohFizJ6NC2EVOvVaTOoyj
7i669/dye5/o4OFARNnhbce8UcNQS6M1UVdcse7Oap9KHpA2orz4D+2zrVXNICyOnH
76TEn8If2OjWohPITsZDimiHhfmPOt8VWzhzD+5mD54W2qu+Q0uGXNSVcdwjxC548w
vvtltmTbeJf4Pa5Hwbjn1rpVmHgj/qI+jB2uOnuuCDfR0S2LrO1JaVHWJ46ZxiWdJr
DfFL+w0SSgd7Ei6y+Vb24s5XQJkPTZB4Zc//ZC7duaceOVRxQe6PDa/f6zLfY2Srbt
qkgbIZSBwSsLw==
Received: from php-smtp4.php.net (localhost [127.0.0.1])
by php-smtp4.php.net (Postfix) with ESMTP id 3981D180387
X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net
X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_40,DKIM_SIGNED,
DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE,
RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no
autolearn_force=no version=4.0.0
X-Spam-Virus: Error (Cannot connect to unix socket
'/var/run/clamav/clamd.ctl': connect: Connection refused)
X-Envelope-From: <rob@bottled.codes>
Received: from fhigh-b2-smtp.messagingengine.com (fhigh-b2-smtp.messagingengine.com [202.12.124.153])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
(No client certificate requested)
by php-smtp4.php.net (Postfix) with ESMTPS
Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45])
by mailfhigh.stl.internal (Postfix) with ESMTP id EC9E82540157;
Received: from phl-imap-09 ([10.202.2.99])
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes;
h=cc:cc:content-type:content-type:date:date:from:from
:in-reply-to:in-reply-to:message-id:mime-version:references
:reply-to:subject:subject:to:to; s=fm1; t=1747311309; x=
1747397709; bh=fMYJo59JHHf14/rs/zEVhR9y1J1GTE48nmATqnHhp/4=; b=M
SbRXuQBC42C1A9OwTFWYczE5b4N9fYfW/kZ+oGMt6gsq/qTSqPspHi1wvTJBoyfe
wlPAS98bu8RCD7fyjjAKifK8+1CG3bbIiZ4EamRfIKYz2b14FCwsZ4W4rsmrB0Ba
FAvUsKbHeGZDbQ5AXBXzmIsyIiGgsUakqC9VkUObfTV704duq+8l76gOjSTxMY4o
VmWFP1EjMIOpeDZnNYfTD8VGFWFQGWEfHRFv8PR0P99hvEQA9nj7xszUiU29eo7q
XoCZ4zqw2bTD9oBmb3ulcqYcVyxmFVUfRyc18hBekYJbDUVXBrr/OKG6TlNYZh4C
GnizE2ZxgduS0puy69GfA==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
messagingengine.com; h=cc:cc:content-type:content-type:date:date
:feedback-id:feedback-id:from:from:in-reply-to:in-reply-to
:message-id:mime-version:references:reply-to:subject:subject:to
:to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=
1747311309; x=1747397709; bh=fMYJo59JHHf14/rs/zEVhR9y1J1GTE48nmA
TqnHhp/4=; b=ujy/emNX3WNiab2gBXgdepaApEpPTR9lhCZfoPCK88VSoNK1GLt
gmbgc7JnWtBt1UY3DzxdDcW9NJPunAJGOGj2gLQSJBd8uLg2T3WsnG8Xxc6uKf5H
jS+4klvtyOh5sioADwY4UMrgrnMGQmTh64DBoiBmXU71Ityw4cgOsfghVllIM3pF
0M+bclCc/YuClYlNPolkfYNgkCJVD1fAJS1Z9oxPv1oiz+BKFSeU0VF28DY9vrch
Hg69CluVP4orIgArJZUD53DaLH9HAPcKrOhyXIanM8WXIqLCpyxG2k01C57vcSzJ
XgKl062IcEiz7c0hzEvJEmyIYWxI6NQ9oFQ==
X-ME-Sender: <xms:zdolaOJzBjnWPQfKIkbwUd451RbYaRj_GBMF_ww8SsZYgQdd1Ia9dA>
<xme:zdolaGJ_KizxMii64fJgqM1QSxtmJIehIJBn6uVy4UDkaW-FF4gjS2nFtAxy1tqSv
SYwt_eC72-BRz1lSMQ>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeftdelkeegucetufdoteggodetrf
dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv
pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih
gvnhhtshculddquddttddmnecujfgurhepofggfffhvfevkfgjfhfutgesrgdtreerredt
jeenucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurd
gtohguvghsqeenucggtffrrghtthgvrhhnpeeiueethedvvdefjefhgfeiheelheehtdfh
feekjefflefgvedvkeduteejjedttdenucevlhhushhtvghrufhiiigvpedtnecurfgrrh
grmhepmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgt
phhtthhopeeipdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehtihhmsegsrghsth
gvlhhsthhurdgsvgdprhgtphhtthhopegrnhgurhgvrghsseguqhigthgvtghhrdhnvght
pdhrtghpthhtohepmhifvghivghrohhphhhinhhnvgihsehgmhgrihhlrdgtohhmpdhrtg
hpthhtohepphhhphdqlhhishhtsheskhhorghlvghphhgrnhhtrdgtohhmpdhrtghpthht
ohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvthdprhgtphhtthhopehvoh
hlkhgvrhesthhiuggvfigrhihsqdhgmhgshhdrtghomh
X-ME-Proxy: <xmx:zdolaOugQl5mz61i032QX1cavyDu-z7r03Rd7zHo_ij7ur66SlF7Ag>
<xmx:zdolaDYSeWbJxJGjIEDJf4zbj9CTenV0gV0-N5nrqSKOvB5oIPFrPA>
<xmx:zdolaFYJ9UFRWUANB7pzB9qQQ-e0jjhEFPf2yW2KuEfgKVWN-YgPyw>
<xmx:zdolaPDZ5uv7Kw4jtFwvc5S3IV9Z5veP-vE8qND16D3QQgPLjN_sBg>
<xmx:zdolaKUL2WK5-WEUsJoGgZ8qHLZuDc72xBQWhkvay8NSC2W_sDmG5gP5>
Feedback-ID: ifab94697:Fastmail
Received: by mailuser.phl.internal (Postfix, from userid 501)
X-Mailer: MessagingEngine.com Webmail Interface
Precedence: bulk
List-help: <mailto:internals+help@lists.php.net
List-unsubscribe: <mailto:internals+unsubscribe@lists.php.net>
List-post: <mailto:internals@lists.php.net>
List-Id: internals.lists.php.net
X-ms-reactions: disallow
MIME-Version: 1.0
X-ThreadId: Ta3b79d818733aba6
To: "Stephen Reay" <php-lists@koalephant.com>,
"Andreas Hennings" <andreas@dqxtech.net>
Cc: "Volker Dusch" <volker@tideways-gmbh.com>,
"Matthew Weier O'Phinney" <mweierophinney@gmail.com>,
"php internals" <internals@lists.php.net>,
Tim Düsterhus <tim@bastelstu.be>
In-Reply-To: <266FA35A-15B0-435E-BBFE-1C6926EB0B7E@koalephant.com>
Content-Type: multipart/alternative;
boundary=a1c3b7ef47ff49c7835b54d079446da8