Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116905 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 381 invoked from network); 21 Jan 2022 09:20:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Jan 2022 09:20:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3905B1804A8 for ; Fri, 21 Jan 2022 02:31:43 -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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 ; Fri, 21 Jan 2022 02:31:39 -0800 (PST) Received: by mail-yb1-f178.google.com with SMTP id c6so26297379ybk.3 for ; Fri, 21 Jan 2022 02:31:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=Hde0nIKocTLqo1bvOoJP1Dx406Kd1kXJpbaFiqZrx4w=; b=LDbikGiSqn8F8/vrBYC8f2tY9a6PddXrK0Jd0V8xWaVnbO+i74SzL1tm3AzTLISqWd 8x7UUXGcWLqFgsQcXUvuVyvgGHEHmVXhWSzLXc4hmhh4KLoBVbQDPgSwxDLczeKZA3gK NTcmMAxruvsKWx2EQ/99tEhOYfxr6yZNbSuHMwEHk06LvEPLx7ZeXST7VhR9duhD4D4v BZNC7uzo646Q8HjEj6Dka0Vp7JWGAI1crFTUkBdCU49k72ekV9RnlZRSJ5SFmTQd0IZO UHxVvsflokN0fhv0Ar+p5VREa0IaMHqTwiFtryHz9UmA1ea00SFj5JAZ3iBYOkEPqybU 4PRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=Hde0nIKocTLqo1bvOoJP1Dx406Kd1kXJpbaFiqZrx4w=; b=hEtuvq9+EReaVo7HvewE6zvH2d2U+EzTuWwOQ3hIuDNlhaF9TdSnDFuz1cBgZwaiaj Pty5aeEps7K4hsv+kIBZ5Ywd2QvNNGja/2ld8EHLaZu+S6MVjG/MXGGo/CX0pp4L0RPq Y72by8TZo2dzimInzQFEi09T0Jhijht39LnL04XCHYb/mpeGa8JxQ1lAgGb4gjgTIIIa 9da8ArN0fWOEP47CPHJ0palIosF9oKArLK+sSFYY6mJRWbQTjvfzO2pIzTUHtMLRQN2X zYYvZK6xWf8hQMSDOq6WjBREJ8avp/DxGWI+3MW7B3asSYbj2qCrjOi7UirGslWJ1pzo bTtg== X-Gm-Message-State: AOAM531MIcrENac+9PegDj0awtdHtgI0VVKkIsMiq6XsEFpT9j1UuPJn Yf6WQHnRrkUK51BMM3Nshom2BQIKUiUFoIONV/7xSWhpOQ== X-Google-Smtp-Source: ABdhPJzsRHQ05T+Npg8R5kvjWsw4ez9tQs9FqzhO0mIOAIDS/HgFoQWxtEEQ7sRPM3vmTV5XYPODLGRT5H9QmDWK1PQ= X-Received: by 2002:a25:4057:: with SMTP id n84mr5578541yba.178.1642761099480; Fri, 21 Jan 2022 02:31:39 -0800 (PST) MIME-Version: 1.0 Date: Fri, 21 Jan 2022 10:31:28 +0000 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Weak Closures From: codekestrel@googlemail.com (Dominic Grostate) 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