Re: Weak Closures

From: Date: Sat, 22 Jan 2022 18:57:43 +0000
Subject: Re: Weak Closures
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-116911@lists.php.net to get a copy of this message
Yes while experimenting I found that solution to work. In order to support $this->func(...) Though I had to use reflection to separate the object and method name. On Sat, 22 Jan 2022, 16:12 Aaron Piotrowski, <aaron@trowski.com> wrote: > > > On Jan 21, 2022, at 4:31 AM, Dominic Grostate < > codekestrel@googlemail.com> wrote: > > > > Hi Internals, > > > > 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 = WeakClosure::fromCallable(function () {}); > > $c = Closure::fromCallable(function () {}, true); > > > > Please let me know your thoughts, because maybe there is a way of > > achieving this with PHP as is. > > > > Kind regards, > > Dominic > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > 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/21bf0cd3d6d6055fc88541e9b24f3140bd179b2d/src/Psl/Ref.php, > but a weak-ref to $this would certainly be more convenient. > > Cheers, > > Aaron Piotrowski > >

« previous php.internals (#116911) next »