Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116911 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85512 invoked from network); 22 Jan 2022 17:46:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jan 2022 17:46:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1CB0A1804BA for ; Sat, 22 Jan 2022 10:57:55 -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.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 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-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) (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 10:57:54 -0800 (PST) Received: by mail-yb1-f182.google.com with SMTP id r65so34356452ybc.11 for ; Sat, 22 Jan 2022 10:57:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=V55kUh2cYWrJdIrrmNFA5qfPz5yyN61yyct1/ADuqhE=; b=Vdenp1gYhjV9yxFCnnLdCIKjt4RJ3VDpCdehNxqk9XhbfuuKJmP+a9UHX4g/RGjfMS XILWwn4fR2l/LB9Ut71T4fu1Pg0urQTw4hzA+aO9dzIi0mpqBciNkQJKyLZ7BcqiLuvD fYixV5qRL0fW7I40s/Gbw27lCfQaEyPiUM0UqjVqjR5PfnQYsRk2gb4oOnGsu75KCVx5 7aL+H5Eu6dpHGD0wsQsx5mtlYzV3vwuZ8v1mYM/TOc/r6GPZuDKkdgwR0QfZMmy2PRsW rRxwGkMuDfgEqVCrWBQKWdSNUA/Oh6pG3TuDFYXwrsU2QKnVxlhvJ+sjJ6+pUg9EuQDV Nq+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=V55kUh2cYWrJdIrrmNFA5qfPz5yyN61yyct1/ADuqhE=; b=pVkHnReulv9W8gCEGmFUqrw/YlPQ737x4PgnnhG8PhI40OqylG5YUf5NycFMQ+IQaW kKx5Osl+dXE9NrSboK197IFh6IDzf29+Z9JssW8P1vzaXHtZ/9/L5EEvIdWlHiSzWoO6 /Y1PycImy4rWWSxfA2/G/Ix/ltg6I+XBGdBg3mRuFDbQg0K69bS2YO2Omq48GUhZ7pje UY3Y4HHoIsUqNC8N5p6scv74sWGCMqW0jsTd4RMLY8kQh5zNpWJKhFBcGu6TsanfR5m1 0bp3UktGgPNnfE8zAlLm6LLbNvpZKUN6fKz6B5nyGF7SjTY4/tDPc+jI1wY054eF9ZUH G9Cg== X-Gm-Message-State: AOAM530Z3gdWPm57bUxQpVU4e4nCUP4XpVzwBNtv5aRTJYmupp2RjvWp 88rUYyDM+Zie+lo2jdgMDdoMzhEJF9WJBj2JE8ojbYU= X-Google-Smtp-Source: ABdhPJytguCEexOOoQWkipU3zGzPGyqJZCIJGi3mz/bEmln5T+J15YAKrbemmvZIS+X5pYelHcWszHvi0YI4xmsODgs= X-Received: by 2002:a05:6902:1107:: with SMTP id o7mr8628175ybu.270.1642877873967; Sat, 22 Jan 2022 10:57:53 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 22 Jan 2022 18:57:43 +0000 Message-ID: To: Aaron Piotrowski Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000016563805d63050cf" Subject: Re: [PHP-DEV] Weak Closures From: codekestrel@googlemail.com (Dominic Grostate) --00000000000016563805d63050cf Content-Type: text/plain; charset="UTF-8" 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, 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 > > --00000000000016563805d63050cf--