-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for closures or at least Closure::__invoke
#3848
Comments
Using |
Does it work now? I didn't check, it wasn't working at the time of opening the issue as mentioned in the OP. |
Cant' call |
@fabpot can you please reopen the issue, apparently it's not yet fixed. |
Calling |
With Twig V3.15.0 I get this:
|
So, your recommendation is to call a reserved method? https://www.php.net/manual/en/language.oop5.magic.php
|
It says that you should not create custom methods that start with I'm not sure what the problem is with that in the context of this issue? |
My point is that they serve to override certain behaviors of PHP, but they are not supposed to be called. You would override Magic method are not supposed to be called by userland code. They are expected to be implemented by userland code, only. TwigPHP enforces users to execute a magic method, instead of honoring PHP's behavior, that is:
|
Thing is it doesn't work, I've explained the issue in the original post. get_class_methods doesn't contain __invoke for closures and hence twig cannot find it. |
@faizanakram99 You offered to submit a PR in the ticket description, please do so if you think you've something that will work. Maybe I'm missing something, but I don't see how it could work without a major performance hit. If I understand correctly, you want |
No that's not what I want, I want {{ obj.__invoke() }} to work, it doesn't since twig cannot find invoke method on closure as it relies on get_class_methods internally. |
Sorry, my mistake then. I tried an object implementing |
Thank you, submitted a PR #4501 |
- fixes twigphp#3848 - adds changelog entry
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
usesget_class_methods
to determine available methods on an object and fallbacks to__call
if it exists and php being phpget_class_methods($closure)
doesn't return__invoke
https://3v4l.org/VKjAF 🤷♂️ 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.
The text was updated successfully, but these errors were encountered: