Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129880 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id F32691A00BC for ; Fri, 23 Jan 2026 09:25:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1769160307; bh=6u9rVItzyGTPHhqYtIM4Sb98G6lZlVPFHYA+NlF4vo8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=XbMO5Zx1CXXCkErvQDVO0TdHilwsyDqPX9CbMVF+U6WsXH7GH466VdQtDLluoXI70 xSwZGOasoGvYe0UYXhcIt9h0IqKmAx/JWzsp0g7KKfDCmve9k3BGFwlQVEuspMvhNi TiMD1hFKkO3IUmqojgr+xphVP3o2WJ7+1mcrkE/7ZOUMPJgKYVfIXstv3AlexigaB0 ZdYQxRb3nxsg+435QOXpL2d3rzs/qx7U4M3WVupEhPQ1CXXzLkYa1wPk+T9TJyVGsu Zt+8NKHLZ7FwGW3bimsfrpnsiiGJLQ26imFKD1Ihin3fOsOB01pznnih3UE0jMJjP9 YRjvtXvYVZu8Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2F651180389 for ; Fri, 23 Jan 2026 09:25:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) 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,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 23 Jan 2026 09:25:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1769160299; bh=nhJiWIcFpLMlGt5gz7+9o/eKR3tfobUcFm1ECICV0s4=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=Rr/02Ko7tGPlqLrAgQ3727Gp7qI7h9EUzjEvcoPOJjXUhc/LsVCVaCmDk11V9Ac1w W9lek5RNBgCMzq2M515jbuYajsSIuoFWe93O+vxbMGMHXha5GuNXJ0/TwLJAir9Wv3 sh1xUufAATWO9xfs193F1+532i2AFc5KRPg67I0N/kLF2ewgEDDS3QKrpZMsV0qHTC lWuVxKjShqgGbxjt2XaMrG/kT8TqX8/dgKkBrx+kRW0fswjYfTMdX8YuqCo9wi7tVM rR+3C3cKLOqYCj+q/+Jj7cFtyhMGar2E6CJ6XHL2wSbB77ktDhV3TEGSQTdFO2HACE xJGPFf/N95YQQ== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 23 Jan 2026 10:24:59 +0100 To: Theodore Brown Cc: php internals , Arnaud Le Blanc Subject: Re: [PHP-DEV] [RFC] Partial Function Application for instance of non-static methods ("$this") In-Reply-To: References: <64953ec741a4c6609519e1878ad37b54@bastelstu.be> Message-ID: <76142b56bbb3ef71dc786c1432ba3c4b@bastelstu.be> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi Am 2026-01-23 00:25, schrieb Theodore Brown: > Thank you for your work on this. However, I'm struggling to see the > benefit > of this syntax over short closures given the examples in the RFC: > > $dates = [ > new DateTimeImmutable('now'), > ]; > > $formattedDates = array_map(DateTimeImmutable::format($this: ?, > "c"), $dates); To provide some background on how this RFC came about: Arnaud and I were discussing my recent `array_map()` optimization (https://github.com/php/php-src/pull/20934) that compiles `array_map()` calls that either use FCC or PFA as the callback into the equivalent `foreach()` loop. The optimization skipped when an actual Closure is passed, since full-blown Closures come with all kinds of scope-related (edge) cases to consider, making it much more complicated. I noted that I believed that with PFA many `array_map()` cases could be handled without a full Closure and that it was unfortunate that it didn't work for “filling in the object itself”. I then came up with the basic idea for the proposed syntax half-a-minute later and we figured it would at least be worth proposing it, since it mapped onto the existing PFA semantics in a fairly straight-forward fashion. > This saves only three characters compared to a short closure: > > $formattedDates = array_map(fn(DateTimeImmutable $d) => > $d->format("c"), $dates); The return type would be missing in that snippet. And depending on your codebase's policy you might also need `static` in front of the short Closure. This might become less relevant with https://news-web.php.net/php.internals/129825, but personally I would still consider it a best-practice to be explicit. Personally I also feel that short Closures have some “visual overhead” in that they don't feel like a simple “method reference”. It's quite similar to how PFA itself is technically redundant with short Closures, but allows to be more explicit with the intention (which enabled the optimization mentioned above). > Furthermore, duplicating the array's type isn't necessary when IDEs and > static > analysis can infer it, so short closures can usually be even more > concise: > > $formattedDates = array_map(fn($d) => $d->format("c"), $dates); This is true when the Closure is immediately consumed, but not when it's intended to be reused at a later point, e.g. with the Symfony Form example where the IDE / Static Analyzer needs some deeper knowledge about how the 2nd and 3rd parameter of `->add()` interact. > I also find the use of `$this` as a named argument confusing. It makes > it > appear as though the current class instance is somehow being passed to > the > method, when this isn't the case. That's fair. I would've hoped to just use `this:` without the `$`, but that comes with the breaking change for variadics mentioned at the bottom of the RFC. > A short closure is barely any longer, and makes it easier to add > additional > logic when needed: > > 'choice_label' => fn(Category $c) => $c->getName(), > > 'choice_label' => fn(Category $c) => strtoupper($c->getName()), Note: Return type + Static also missing here. And of course if more parameters would be passed, it would scale differently (since more types are required for proper autocompletion). Best regards Tim Düsterhus