Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119392 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 69943 invoked from network); 22 Jan 2023 17:45:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Jan 2023 17:45:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 500A31804F7 for ; Sun, 22 Jan 2023 09:45:47 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (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 ; Sun, 22 Jan 2023 09:45:45 -0800 (PST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 7DE785C014D for ; Sun, 22 Jan 2023 12:45:45 -0500 (EST) Received: from imap44 ([10.202.2.94]) by compute1.internal (MEProxy); Sun, 22 Jan 2023 12:45:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ollie.codes; h= cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm3; t= 1674409545; x=1674495945; bh=QG5Gv7eSj8z7myMRc/GMzKMGgLKM/Ya/7Rs JjrNraQc=; b=ezgbHRWx+/k5B85Lj3i2O55IbkN47Np0oa8o0tJsmdFE2OKFPEU oeeKekN8rPtOPoSWJRderbyqNizO1SkPhJTnkyd0pW97qwYbf29xHejCjjOlUsov nVqSk2oHcz/rvPRyp+xF4+Z4E3VesOSf8IOApfl7IYAb5/UdZiGGqg1fXYTHBn35 gk5g0PqPweCeHPTV842CCqJQzLNhNgHwdTjbuOvWoiigoShXysO/Jqk2IQks4s6m GAxaRnRMWUfOlUPK0w5OA+6IU832CuE3wXX7APXr2Wl7oxWTE4aj1JayFfu+n+/s +8yatrU1UARamK4p7PAUm0KWY5xCWj3jizQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:feedback-id :feedback-id:from:from:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1674409545; x= 1674495945; bh=QG5Gv7eSj8z7myMRc/GMzKMGgLKM/Ya/7RsJjrNraQc=; b=F sWpzhRMvemSIrp/tQZCQKKIpwQuF/jvkcrgukCzMyI9rzEZbz3jPcBXosfT6+uZn Nlcpmk8dscMQHlwoa4MJYohaZ0Q1rDHexP0/ABTlE0O9FiY4Bdt2WHL7drWeG8aN AlH/M2HvNATC4ygqnInzpLg8NzXL6GWy7EpSGCT1X3xhOvZ2jK8h6RC7QnwyefeE iYsAG3LW9srCzpy1MHldJZZudwhhzqpdmQ14Ulp8UjCcyjoT1fXRfju92FlTcsuc yVFpa+1MAkdsfxm5psgG6HPUociU6+G+xpNLmvKt/2MkiSsieN4G3rwbQXullrHS wTX+zlRLGBF+mpBT7UmJA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudduiedguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkfffhvffutgesrgdtre erreertdenucfhrhhomhepfdfqlhhlihgvucftvggrugdfuceophhhphesohhllhhivgdr tghouggvsheqnecuggftrfgrthhtvghrnhepgefhteekgeehieejtdelleeivdffuddtte euteeufffhfeejveevffeuhfdugfelnecuffhomhgrihhnpehgihhthhhusgdrtghomhen ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehphhhpse holhhlihgvrdgtohguvghs X-ME-Proxy: Feedback-ID: i835c46d5:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 3E32236A0073; Sun, 22 Jan 2023 12:45:45 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-85-gd6d859e0cf-fm-20230116.001-gd6d859e0 Mime-Version: 1.0 Message-ID: <8f530a4a-bf48-4259-877e-16ebf8082936@app.fastmail.com> Date: Sun, 22 Jan 2023 17:45:22 +0000 To: =?UTF-8?Q?Bj=C3=B6rn_Larsson?= Content-Type: multipart/alternative; boundary=f79983079fd048f6bad71ed2c5907e7e Subject: Introduce the abiltiy to use the first-call-callable syntax on non-static methods, statically From: php@ollie.codes ("Ollie Read") --f79983079fd048f6bad71ed2c5907e7e Content-Type: text/plain Hello all, I've created a feature request issue on GitHub (here: https://github.com/php/php-src/issues/10414), but I have been advised that it's best to post here. What I would like to introduce/suggest, is the ability to create a closure from a method using the first-class-callable syntax (eg: MyClass::aMethod(...)), for a non-static method, statically. Currently, the following code causes an error. ``` class Test { public function test(): string { return 'test'; } } $closure = Test::test(...); ``` I understand why the error is thrown, but, and I'm unsure of the specifics regarding this, I think we could delay the error until the closure was called. The reason for this, is that closures can be bound, so if you followed on from the code above, you could do the following: ``` $closure->bindTo(new Test); $closure(); ``` The above would bind the closure in $closure to the scope of an object, which in this case, is the class that the method belongs to. The best example I can think, for this, would be when filter a collection of instances. If you were using a collection library, you would currently have something like the following: ``` $collection->filter(function (Str $string) { return !$string->empty(); }); ``` Whereas it would be much nicer to have the following: ``` $collection->filter(Str::empty(...)); ``` In this situation, the collection library would be responsible for binding the closure to the value it is iterating. I have limited experience with PHPs source, and C in general, but my understanding would be that if we were creating a closure, we would skip the check for the static method. The code responsible for handling the closure call would most require some additional functionality to check if it was bound to a valid instance, returning an error if it isn't, and then returning an error if it isn't bound at all and the method isn't static. The more I think about it, the more I think this may require a new type of Closure, or at least a runtime applied interface, to help developers determine whether a closure was created using first-class-callable syntax. I'm interested in feedback on this, and if anyone could point me towards the correct part of the codebase that handles this, I can investigate further. I am happy to create an RFC if others think this viable. --- Best Regards, *Ollie Read* --f79983079fd048f6bad71ed2c5907e7e--