Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113825 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17984 invoked from network); 28 Mar 2021 11:33:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2021 11:33:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 21A211804DF for ; Sun, 28 Mar 2021 04:29:50 -0700 (PDT) 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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (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 ; Sun, 28 Mar 2021 04:29:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1616930987; bh=rm03rWUJshBvZ6Itk3+FXznbV7Nb9f27L/xJkvi/Dvk=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=V4XbvM3M4VZeCYQLSUL7pYXgilKdMl34ImLl5uyBHDd62MsmdTKv3DyM7jeBD8LfI HoN7nqCV2gwKO7Jzl6Ok4VKiLTkcL43ajLfDIglCsYIImL+iorGBTXR6BfomdMpfLF EZeNecHkSu7ke3+pPWOW1rFpbqyQa+SWseQhMgLw= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [172.20.10.9] ([178.197.234.24]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M8ygO-1lWY9t1i5u-0066cZ for ; Sun, 28 Mar 2021 13:29:47 +0200 To: internals@lists.php.net References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <919e30e7-3e5e-d955-7bb4-1e1b5825cdd1@gmail.com> <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> <36E45DD6-E2BD-4801-BAAE-4355C83D1AC3@newclarity.net> <15AE4315-A456-4ED8-990A-49EBD76C5B46@newclarity.net> <5501FE70-FBED-47EB-8010-173644BC064F@newclarity.net> <3d453ce7-db16-fb75-91b4-9a2a71994164@gmail.com> Message-ID: <8b4ed241-2f74-f801-ccdc-fde6c68ff612@gmx.net> Date: Sun, 28 Mar 2021 13:29:41 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: <3d453ce7-db16-fb75-91b4-9a2a71994164@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Provags-ID: V03:K1:m6XDofB2oG9zH721UCZvj2Ltoh/PnWaKRPfhZRbY9jMUatgW6hh 9vYoEZ/RAXYxFmpba1tevag3vPIwdUH2ueiaipU+3EuZet0Vd+SdbPwHfF16IuB1GxPu4Wd CVR4ZiO1rtxYUx2t8NRDwSHG7cgQn6qcUHhHhU/jyzn0eUtowVDLJQsdd5EWW0mrzrXadQu drMpfXVA+m1LFu+4Wdw/g== X-UI-Out-Filterresults: notjunk:1;V03:K0:LYvldkf/Sqo=:6HD9BBDcd0EGmh9nknDEHy rsNX7g9P0LhGAkNUQvqMUTqosCPH/fz921NpkFykLM9sK51xCKEvHbNQirCwtwqeTkrTvJT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HwdN/wNFdnv2/BePIACbATnpCSj5Q l5YyHGlKyS+VViyt/ejCKz9dnU++Gj2tqOJc+wmWMp479oh4PydWd92P1HXNX/FxmbZdY= Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: a.leathley@gmx.net (Andreas Leathley) On 27.03.21 17:05, Rowan Tommins wrote: > My biggest concern with automatic capture is the potential for > *accidentally* capturing variables - that is, intending to introduce a > local variable inside the closure, but capturing a variable from the > outer scope that happens to have the same name. This is less of an > issue with capture by value, but can still mean resources not being > freed, e.g. a large array of data not being freed from memory, or an > object destructor not executing when expected. > > This is more likely in PHP than many other languages, because there is > no requirement, or even an option, to explicitly declare a local > variable in the closure. It's not a new problem, but since > single-expression closures are unlikely to use many local variables, > it's probably not one that's been given lots of thought. I do think it is great that you are thinking about how this can negatively effect memory or performance (and things like destructors) and if things can be improved. But do the same problems not exist when binding the current class to an anonymous function - whenever an anonymous function is declared in a class (no matter if by function or fn) the whole class is bound to that function, if you don't explicitely use "static function". That binding can encompass many variables and other classes etc., which is common nowadays with dependency injection, yet I have not heard much about problems surrounding this. Binding the variables from the current scope seems like a smaller impact compared to already binding the whole class and all its dependencies. I would welcome any way of automatically capturing all local variables, as this would be a big "quality-of-life" improvement similar to constructor property promotion, to avoid a lot of boilerplate code. I would not use it that much, but when I use it, it would be a big improvement. Using "fn" for this seems consistent to me, and I don't think people would always use it just because it is shorter - and IDEs and static analyzers could detect and discourage use of "fn" if "function" would be sufficient.