Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117894 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97256 invoked from network); 9 Jun 2022 17:42:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2022 17:42:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 457271804D4 for ; Thu, 9 Jun 2022 12:29:12 -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.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, T_SCC_BODY_TEXT_LINE 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-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 9 Jun 2022 12:29:11 -0700 (PDT) Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-2ef5380669cso251709677b3.9 for ; Thu, 09 Jun 2022 12:29:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=p10ExVjGnoOD5+a4nR3v3FCSj7xgARRRczY/yLdvKH4=; b=ZL5cC6G8O14zAiZt7F3+Mq78J/+YghUz2Rug/XHREK04dr0MQOXLaxtngYRTZdDLEm Rg6INYv8CYb39aKo6I9DFk4/9w9VB+ZCGHYon+JbsPr8rs2UrvnZJCb+Zhm4GVThhiVC ttUmtXRYrZM21vao0J2GBtTStTy/vpv0uibO+7q3XXrNuMg3WgjrISeCxfS8O3OEvf7M TzsoN2pbCchfhLfv36YGR6rhp5eizG2gq8QBqGxikEpGmk+sR7Kn7HTdd2jXehZ7lyZv x2hxf++GBsQMsPyw+Hhptkf6F8B/m0fkDrNxaOZtdMjLKWh6gk1Ed5LHg5/0EPI84TwF dP5Q== 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=p10ExVjGnoOD5+a4nR3v3FCSj7xgARRRczY/yLdvKH4=; b=4mSLcIit2DDNHxkvKtbZWx0C7XW9CZlVI0pPZz4lSU7f/WajtFR6pWrLrqY8o2ZUSb 6Gg8g1sdYDHjJvtrY2CfhL22BE3z+s4ES/1z24ERktIV7bUUHkn3CCcaSYyKacSkcgRI TKP3dSYicg0o+6a3BEfAUPXk3ttbKTgHJ9ybAtqMX1pczAOu7qe2ZxrCoEBdI0aJCF2M uDdhgS4cKYA15HPw8kMQyLHxKU5kuAmAcbVuGqEwLu6t/Sh029y5MssVXmffcPva3HG7 BUsJogXUFnEs2QaW1Yu+NwsLM7GWM9B7L26tGi9ZLan9/DCjrin4VwyV0G6aaslcqUBO Y13Q== X-Gm-Message-State: AOAM5302gYoAtZYLvZqgeofKXyNfM19XX+PJ8eFAcixaCyNyF4Z1serH SjZcGNYS5P6SY6Y6WnoFp1L/dMSrnWacHuqjoXe/eXhr0EE6KQ== X-Google-Smtp-Source: ABdhPJwAQnf6fMkYogspYyACfluTJea8mC783NOez9KxzUE27OByOhxGAzs7xaAdRCMb/BaVIjboWh5X+emPf1dz+hU= X-Received: by 2002:a81:6ed5:0:b0:313:9109:b86 with SMTP id j204-20020a816ed5000000b0031391090b86mr5167700ywc.509.1654802951173; Thu, 09 Jun 2022 12:29:11 -0700 (PDT) MIME-Version: 1.0 References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <21891029.EfDdHjke4D@arnaud-t490> In-Reply-To: Date: Thu, 9 Jun 2022 21:28:59 +0200 Message-ID: To: Nikita Popov Cc: Arnaud Le Blanc , Larry Garfield , PHP Internals List Content-Type: multipart/alternative; boundary="00000000000013f8aa05e108d622" Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: ocramius@gmail.com (Marco Pivetta) --00000000000013f8aa05e108d622 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Jun 2022 at 21:27, Nikita Popov wrote: > On Thu, Jun 9, 2022 at 8:15 PM Arnaud Le Blanc > wrote: > >> Hi, >> >> On jeudi 9 juin 2022 18:46:53 CEST Marco Pivetta wrote: >> > ## nesting these functions within each other >> > >> > What happens when/if we nest these functions? Take this minimal example: >> > >> > ```php >> > $a = 'hello world'; >> > >> > (fn () { >> > (fn () { >> > echo $a; >> > })(); >> > })(); >> > ``` >> >> Capture bubbles up. When an inner function uses a variable, the outer >> function >> in fact uses it too, so it's captured by both functions, by-value. >> >> This example prints "hello world": The inner function captures $a from >> the >> outer function, which captures $a from its declaring scope. >> >> This is equivalent to >> >> ```php >> (function () use ($a) { >> (function () use ($a) { >> echo $a; >> })(); >> })(); >> ``` >> >> > ## capturing `$this` >> > >> > In the past (also present), I had to type `static fn () => ...` or >> `static >> > function () { ...` all over the place, to avoid implicitly binding >> `$this` >> > to a closure, causing hidden memory leaks. >> > >> > Assuming following: >> > >> > * these new closures could capture `$this` automatically, once detected >> > * these new closures can optimize away unnecessary variables that >> aren't >> > captured >> > >> > Would that allow us to get rid of `static fn () {` declarations, when >> > creating one of these closures in an instance method context? >> >> It would be great to get rid of this, but ideally this would apply to >> Arrow >> Functions and Anonymous Functions as well. This could be a separate RFC. >> > > I've tried this in the past, and this is not possible due to implicit > $this uses. See > https://wiki.php.net/rfc/arrow_functions_v2#this_binding_and_static_arrow_functions > for a brief note on this. The tl;dr is that if your closure does "fn() => > Foo::bar()" and Foo happens to be a parent of your current scope and bar() > a non-static method, then this performs a scoped instance call that > inherits $this. Not binding $this here would result in an Error exception, > but the compiler doesn't have any way to know that $this needs to be bound. > > Regards, > Nikita > Hey Nikita, Do you have another example? Calling instance methods statically is... well... deserving a hard crash :| Marco Pivetta https://twitter.com/Ocramius https://ocramius.github.io/ --00000000000013f8aa05e108d622--