php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79369 Syntactic sugar for Closures ($object->method::function)
Submitted: 2020-03-11 17:46 UTC Modified: 2020-03-11 23:12 UTC
From: txigreman at hotmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-03-11 17:46 UTC] 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).



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-11 17:52 UTC] nikic@php.net
-Package: PHP Language Specification +Package: *General Issues
 [2020-03-11 22:06 UTC] 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 23:12 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2020-03-12 09:10 UTC] txigreman at hotmail dot com
Hi Rowan,

Thanks for your reply.

I've readed the externals link, and subscribed to the internals mailing list. Really hoping to colaborate.
How can I write to the chain? Do I Have to wait to get an email and then reply to it?
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Dec 03 13:01:23 2020 UTC