Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116907 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73811 invoked from network); 22 Jan 2022 15:01:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jan 2022 15:01:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id ADAEA18033A for ; Sat, 22 Jan 2022 08:12:53 -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=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS21554 199.38.80.0/21 X-Spam-Virus: No X-Envelope-From: Received: from mercury.negativeion.net (mercury.negativeion.net [199.38.81.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 22 Jan 2022 08:12:53 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mercury.negativeion.net (Postfix) with ESMTP id 6E25D21034122C; Sat, 22 Jan 2022 11:12:52 -0500 (EST) Received: from mercury.negativeion.net ([127.0.0.1]) by localhost (mercury.negativeion.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id z6WGwILnXJyT; Sat, 22 Jan 2022 11:12:51 -0500 (EST) Received: from smtpclient.apple (unknown [173.225.146.47]) by mercury.negativeion.net (Postfix) with ESMTPSA id 7453E210341218; Sat, 22 Jan 2022 11:12:51 -0500 (EST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.40.0.1.81\)) In-Reply-To: Date: Sat, 22 Jan 2022 10:12:50 -0600 Cc: PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: References: To: Dominic Grostate X-Mailer: Apple Mail (2.3693.40.0.1.81) Subject: Re: [PHP-DEV] Weak Closures From: aaron@trowski.com (Aaron Piotrowski) > On Jan 21, 2022, at 4:31 AM, Dominic Grostate = wrote: >=20 > Hi Internals, >=20 > 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). >=20 > Consider the following example: > https://gist.github.com/orolyn/7651e4127759aad1736547490baa1394 >=20 > 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. >=20 > 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. >=20 > However if this is intentional, I would like the option to make the > closure weak for example: >=20 > $c =3D WeakClosure::fromCallable(function () {}); > $c =3D Closure::fromCallable(function () {}, true); >=20 > Please let me know your thoughts, because maybe there is a way of > achieving this with PHP as is. >=20 > Kind regards, > Dominic >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20 Hi Dominic, Implementing a weak closure is possible in user code. We've done so in = Amp v3: = https://github.com/amphp/amp/blob/379177aba93518e2df6d626677cbbdc48cc0d8ae= /src/functions.php#L119-L171 I would be in favor of having this functionality available directly in = PHP. Amp often uses references to object properties in static closures = to avoid circular references to $this. There are other ways to = accomplish this, such as a ref object like = https://github.com/azjezz/psl/blob/21bf0cd3d6d6055fc88541e9b24f3140bd179b2= d/src/Psl/Ref.php, but a weak-ref to $this would certainly be more = convenient. Cheers, Aaron Piotrowski