Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116906 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70474 invoked from network); 22 Jan 2022 14:18:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jan 2022 14:18:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B7461180089 for ; Sat, 22 Jan 2022 07:30:26 -0800 (PST) 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.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 22 Jan 2022 07:30:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1642865423; bh=87YfRdiZ3qmlTZ88u8/ALPVXCxqr0J62e8i+4ZBxAiA=; h=X-UI-Sender-Class:Date:Subject:To:References:From:In-Reply-To; b=KHZJSPj4ErHr+7fgLvb+6I2b4AJ/EYWrikyJP4P2fgNamD2jI5Fqi92vWALxJTSZX x8rhexNFV/sNvWfketcvGRgzoJCdQ26jJ9zPmh27wdzuufAhqNXdnQ1JjmMENlqu16 4i+R/HEWHNr/9dQb9FEr43Nyl45V0YcNK8SigXo8= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.2.130] ([84.179.224.47]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MoO6C-1mVcwh27Zo-00omCW; Sat, 22 Jan 2022 16:30:23 +0100 Message-ID: <7ea94d3b-7f1f-b604-b94a-781e0b992c69@gmx.de> Date: Sat, 22 Jan 2022 16:30:22 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: de-DE To: Dominic Grostate , PHP internals References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:EooxgNxgXekB7E1xS6FkaZEzHQPj8pWKWm1Kg5PaG7cGJkIzCFK x0tluzEQbNfok20BDkkf4yAwvsASDHMBi2kbcZczB73TR3uHMSY49RnbJrD+mGxas3VGpdy XmyMALG9THfDtba0P94xxUHEMa+/wmS3Fsp2hkc7hFf6UM6c1I4b7ztydHCBajH7TWJMwEO RRymuByIzvOr8FPwRUdZg== X-UI-Out-Filterresults: notjunk:1;V03:K0:KSDadskGKKk=:HKG/VUjyiOCd6iCiUTpDFB Nw+cJnS4i/VnubwCpLblbULuCOM2g4lt3Z0cm3R6Npkr8VAv3j75D94RAgVvy8Emd/4VETQxc XdR59Er+wNovXJGnNAEMMG5Em6qxOOyk6+7Dy4WMxJc+gCv23woXfTtPy5q6EyhJaPMl+v7Y2 iOet6zce1QdK45SRxJMAiuqop2B1AgthGQ2xQURfoAkcp4cHcHryEgcRxWpBSMn6czcYYqm/J JwUruinDkfw+dTzcqYHOEG3+xjzQ3CGg5vVcfgiJUoqPkFqSXTIOLLbyKIX+zw66QwzkokmJY CcCSEgxmtMRLwgloFfwfE3HB6Zd1V5OI0JfEGz9RkUmFX6MdC1vCetjquPooHH4FgUsykh1nV LjK+pQMxI5uATd2zhF+yWGRS1+2NwdF14yUAE9DfeEKWjWfqcubncG7xMUpHCrv2yENFMvEnK k7aOHlmKHnN7NRU9Kq3C7lqOwEfyhoG7tnc4nynOINLMzB8zgnQsJigkBdgA8cTtJauPdMFxp FJjhx16q/QtmlhOykfECH/XbyYBiCCWxUf5d32HFjGbtLVZzTqqrffKcMoK1ZqPB3y1Kkr4bl 9vpUSpwBCgZrydCGC2M1oKHy6H7UGt0i2VBm7DtAgfZRFroJc+AoDBS3YEuB551XfbcgF1aHK 9QfgRUM6xAwxRQeYPdUYh91PhzlyMHG7u1wYn5L5Aa6nhYKMTPGuA6MJL2sSCx9IewNl6eEno rA2Z0yFPyIcJnu7tZ8DY+SLmBk/oa5wi2Dv5U7vM85PsMC38rVGMjPZV1saUCgMb3vp6QNJ8U KoYMdCa7CQAI7t7xqkTxfW4A/2epUhvbPvbH45A/8H9tC4bdakBXiIQTutWyafyrVZI/a98cR 18hFCw2OnmDd4g0Q3xNXpmv1r6wL/Tlg0jj7pLwAC/e+xyiLO5ssn03qwHQrBnLPfFTb5Pk7o hC0Sj5zfCM3a2BgnHEQ4A6QRq1HaBu21qgbxp2QMwj7+hT9N3uPljNMxStyATMlBkVeqHQ84c ftJUHPF/mZkZCYm3nn/FsiE86xhN8OgU2YJGm5/suJNzj0M3WSmTDpkdO8yexZJVxGL8PbnZe n96U29wJgCGNKY= Subject: Re: Weak Closures From: cmbecker69@gmx.de ("Christoph M. Becker") On 21.01.2022 at 11:31, Dominic Grostate wrote: > I'd like to express my interest in a possible feature concerning weak > references. Currently closures created within a class appear to > contain a reference to the object that created it. This is of course > necessary in order for the closure to retain the necessary scope. > However I would like to suggest we have the option for closure to > weakly reference this object so that when the object is garbage > collected, the closure too may be rendered null or invalid > (inspectable). > > Consider the following example: > https://gist.github.com/orolyn/7651e4127759aad1736547490baa1394 > > The idea here is that without unset($sample) the loop would run > forever, because there is a reference to the callback from the object, > and in turn there is a reference to the object from the main stack. > With the unset($sample), the idea is that before the callback is even > called, the reference to the object is destroyed thusly so is the > reference to callback, and since the callback is only referenced now > in a WeakMap, the callback will finally be gone. > > In reality it appears there is a circular reference between the object > and the callback. I don't know if this is a bug or not, because from > what I can find PHP was fixed a long time ago to resolve circular > references. > > However if this is intentional, I would like the option to make the > closure weak for example: > > $c =3D WeakClosure::fromCallable(function () {}); > $c =3D Closure::fromCallable(function () {}, true); > > Please let me know your thoughts, because maybe there is a way of > achieving this with PHP as is. If you trigger the garbage collector manually (i.e. call gc_collect_cycles() after unset($callback)), the loop terminates right away. I'm not sure why it doesn't without manually triggering the GC. Christoph