|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71842 Uniquely identifying \Closure instances
Submitted: 2016-03-17 07:59 UTC Modified: 2016-03-18 14:58 UTC
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: kjarli at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: 7.0.4 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-03-17 07:59 UTC] kjarli at gmail dot com
A lot of web applications feature controllers. A controller can be really anything in the form of:
 - A method in a class
 - An invokable class
 - A function
 - A closure

Now what is being doing in Frameworks like Symfony, is that you can have arguments of the action methods injected with some reflection with some fancy resolving if you please. Even though using reflection during run-time, it's a feature that cannot be missed for the purpose of DX (Developer eXperience).

To make this faster, I was thinking of caching away the arguments resolved by reflection, this includes name, type (compat between 5 and 7), variadic (false on 5.5), has default and default value.

This works fine for pretty much everything _except_ anonymous functions. In order to identity a controller, I can save the class+method or function name. For closures this is not possible because everything is a \Closure and the object hash is no reliable across requests. This means that for anonymous functions I would still have to resolve run-time what the arguments of the function are. With php 7 supporting Anonymous classes will have the same issue.

Would it be possible to generate a signature hash of somesort so you can detect the same anonymous class/function across requests?


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-18 14:58 UTC]
One thing that I've heard people discuss before, is that it would be useful to allow getting information from a closure of how/where it was generated.

e.g. for anonymous functions, the file and line that they were defined in, or for closures generated from ReflectionMethod, the source class and method could be returned.

That information should satisfy your use case, as the information should be unique per closure, I think.

It would also be useful for debugging, as currently if you have an error in a closure, it can be hard to find where it came from.

This would probably need an RFC to be passed as it needs to be thought about. And there could be reasons not to do it, e.g. adding memory overhead for little gain, but it doesn't sound like a difficult thing to add.
 [2016-05-27 01:45 UTC]
I see annotations or attributes as a good and generic approach to solve this issue:

$router->add('/hello/{param}', <<route(hello)>> function($param){
   return 'response';

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 25 16:01:31 2024 UTC