Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Support for closures or at least `Closure::__invoke`

Open faizanakram99 opened this issue 2 years ago • 0 comments

Calling a closure is not supported in twig right now, any variable passed as \Closure cannot be called directly as Twig looks up the function in global functions (and extensions), the alternative is to call it via __invoke method on closure.

https://3v4l.org/EBR2Y (__invoke method does exist on Closures and work the same way as $closure() works).

Unfortunately this doesn't work in twig as twig_get_attribute uses get_class_methods to determine available methods on an object and fallbacks to __call if it exists and php being php get_class_methods($closure) doesn't return __invoke https://3v4l.org/VKjAF :man_shrugging: even though it exists and is documented.

I think this can be fixed by checking if $object is instance of \Closure and then allow calling __invoke or is there a better way ?

I am happy to contribute a PR if it makes sense.

faizanakram99 avatar Jun 07 '23 15:06 faizanakram99