Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120135 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46090 invoked from network); 26 Apr 2023 07:04:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Apr 2023 07:04:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 97850180382 for ; Wed, 26 Apr 2023 00:04:15 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 26 Apr 2023 00:04:15 -0700 (PDT) Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-2febac9cacdso4044877f8f.1 for ; Wed, 26 Apr 2023 00:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682492653; x=1685084653; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=1I7zjacoBlZQr5sHhao056zdzRXkQW2AXJyhgzs/WOM=; b=Qd4i8aO/hXIwmEWVV/S6rB6kpfxzXaQy8C+fA0BIY1L1t5m4owFFFfN+NdgG+X+1Pf zHMeE1hRX6liPAt2n3W4nQR3HlB7Ov55WhvIDe7nzqcdm/+rCpS6M7VNXesO2S/Vr5Y7 hoJCAryOQxQfd3voxbGN8u+vXzHfPSpp7omURIBGD0I9YFFsxNc5zk3jRmB22fRihdUc QJX32i/mGmJ5po1OL1wlTcaETjERv3CcJ/YDY2e4cPqOAuO4U3sq337cSt3Ggz8TyG47 wmeTR5bcAQHiDRKNC9fS6a2rsEoChl4l3TFFoySjBaCgYxRquDVbSdiJMwwrJ0EXaWj0 FDgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682492653; x=1685084653; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1I7zjacoBlZQr5sHhao056zdzRXkQW2AXJyhgzs/WOM=; b=ckBflbXh5+LNzBMQQirPiqYBNmemBeE9Yx6N+azRyvZNWTUWC7e22HgEuYWbyVmHJq QI4rQINF66sTXIedShlNNvYC+esW2bU+D9KwnGYSqNqEDYtJx9G8csBqrV3ABpCKFi/1 y9cX/BqBZBOmr8u0I9Lo1CAmzq0qoP9THHkV79U9llp7Qgcab2jvTGFso8wB3UbfTg0d ht2L15LzqL6dE8bkG8vSklpYcEnSooPVV5QC0WttsppUSDV2FLmTlXcHuAVxvSIzUD4Z XHTF1pqRATC+pT92NbtVLBHcEbrWJRriDWyoEag4qM4fznpei3My4KbdvzJ+SiV+3wwd bKPA== X-Gm-Message-State: AAQBX9cl4y7N/Cs8M1SVv9xGzRPb9erpywQyt0MuoBRqBEtcvFXzQcd2 yG6TEKteqN8y7pvsjKDJMy9WU9CQRgtDBcZdpK6Qrpbd8JTwtA== X-Google-Smtp-Source: AKy350a5S2FMVzJw3nMSIaq1eSE4k9BrvSRl/RNQvJT5EIQVvIfDiRey43NA15JmocSbGgrepAFihuCq1gxpjGeGZcU= X-Received: by 2002:adf:e445:0:b0:301:8551:446f with SMTP id t5-20020adfe445000000b003018551446fmr13290718wrm.38.1682492653181; Wed, 26 Apr 2023 00:04:13 -0700 (PDT) MIME-Version: 1.0 References: <799ae864-6e25-4196-a5ce-0d74600a8378@app.fastmail.com> In-Reply-To: Date: Wed, 26 Apr 2023 09:04:01 +0200 Message-ID: To: =?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?= Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="000000000000ee1ea205fa37d88e" Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000ee1ea205fa37d88e Content-Type: text/plain; charset="UTF-8" > > > What about using a real closure to define the scope we need for > cloning? > > > That closure would take the cloned instance as argument to allow > > > manipulating it at will. > > > > I believe someone mentioned that one previously in the thread. > > > Yes, Nicolas mentioned me. > I wanted to get back myself to discussing this topic more as well and find > a better solution but didn't get yet time for it. > > > > The problem is that the closure would run in the scope of the object, > > not the scope of the caller. That means if called outside the object, it > > would allow modifying private or protected properties. The itemized list > > of values (whether an array or named-args style) would allow the engine > to > > enforce access restrictions, which is a desireable feature. > > > > As far as I can see, Nicolas was able to find a solution to this problem > and so far I like it: > The closure is running in the current scope where it is defined and not in > the scope of the cloned object. The cloned object is passed as the single > parameter to the closure. > Absolutely. This would be a plain boring closure with all its current visibility semantics. Using a closure to run some code nested in a transaction is already quite a common practice. E.g.this is a common way to define the computation logic for a cache storage: $cache->get($cacheKey, $callback) Or for a database transaction: $db->transaction(function() { ... }); The cloning logic we want to run fits this style, so this is quite natural once we realize that: $clone = clone($this, $callback); The only thing we would need to settle on is the interface of the $callback. The suggested clone signature for a class T would be: > - clone(T $object, callable(T $clone)): T; // calling clone as a function > - clone T $object with callable(T $clone): T; // calling clone as a > language construct 100% this. My preference goes for #1, to keep things as boring as possible. Just to make it clear, I would document the closure with the void return type: clone(T $object, callable(T $clone)*:void*): T; // calling clone as a function > Alternatively, we can have also: > - clone T, callable(T $clone); // without "with" keyword > This would be ambiguous, eg foo(clone T, callable(T $clone)) is that a function call with two arguments, or? > And improve it to allow even multiple closures to be executed: > - clone(T $object, ...callable(T $clone)): T; > - clone T $object, ...callable(T $clone): T; > I wouldn't allow this. Calling many closures is what the main closure can do in its body, no need for more fancy things. Nicolas --000000000000ee1ea205fa37d88e--