diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index d3d637c05e..01e7285669 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1740,6 +1740,10 @@ public static function getAttribute(Environment $env, Source $source, $object, $ static $propertyCheckers = []; + if ($object instanceof \Closure && '__invoke' === $item) { + return $isDefinedTest ? true : $object(); + } + if (isset($object->$item) || ($propertyCheckers[$object::class][$item] ??= self::getPropertyChecker($object::class, $item))($object, $item) ) { diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 811f4f5deb..eb0e2db7d5 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -396,6 +396,7 @@ public static function getGetAttributeTests() // test for Closure::__invoke() $tests[] = [true, 'closure called', fn (): string => 'closure called', '__invoke', [], $anyType]; + $tests[] = [true, 'closure called', fn (): string => 'closure called', '__invoke', [], $methodType]; // tests when input is not an array or object $tests = array_merge($tests, [