Req #79369 [Opn->Sus]: Syntactic sugar for Closures ($object->method::function)
| From: | requinix@php.net | Date: | Wed, 11 Mar 2020 23:12:28 +0000 |
| Subject: | Req #79369 [Opn->Sus]: Syntactic sugar for Closures ($object->method::function) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226043@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79369&edit=1
ID: 79369
Updated by: requinix@php.net
Reported by: txigreman at hotmail dot com
Summary: Syntactic sugar for Closures
($object->method::function)
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2020-03-11 22:06:26] rowan dot collins at gmail dot com
Hi,
This kind of broad feature request is generally best raised on the PHP Internals mailing list
(https://www.php.net/mailing-lists.php) which gets a lot more attention than this bug tracker.
As it happens, there was a discussion on precisely this feature last month! You can see an archive
here: https://externals.io/message/108459
The main points of discussion were:
* Should it return a string (like ::class does) or a closure?
* Function names are resolved at run-time, so it couldn't be processed at compile-time like
::class (mostly) is (this also means we can't have a single ::nameof syntax)
* A few alternative syntaxes were suggested, particularly to better handle instance and static
methods.
Thanks for getting involved. :)
Rowan Tommins
[IMSoP]
------------------------------------------------------------------------
[2020-03-11 17:46:13] txigreman at hotmail dot com
Description:
------------
Hi,
Actually, you can can create a Closure with any of this:
$closure = function() { return $this->method(); }
$closure = fn() => $this->method();
$closure = \Closure::fromCallable([$this, 'method']);
I think it would be greate to have a simpler way to create a Closure. I suggest to add a simpler
variant:
$closure = $this->method::function;
$closure = Class::staticMethod::function;
$closure = trim::function;
It mimics the functionality from MyClass:class and $object::class, so the syntax is user-friendly.
Additionally, it does not uses a new keywork, so I think it has no backwards compatibility breaks
(or so I hope).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79369&edit=1