{"record":{"id":"52f32ee8083062b5","repo":"twigphp/Twig","slug":"impossible-to-invoke-a-method-on-a-variable-dynamic-message","errorCode":null,"errorMessage":"Impossible to invoke a method on a variable (dynamic message).","messagePattern":"Impossible to invoke a method on a variable \\(dynamic message\\)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1846,"sourceCode":"\n        if (!\\is_object($object)) {\n            if ($isDefinedTest) {\n                return false;\n            }\n\n            if ($ignoreStrictCheck || !$env->isStrictVariables()) {\n                return;\n            }\n\n            if (null === $object) {\n                $message = \\sprintf('Impossible to invoke a method (\"%s\") on a null variable.', $item);\n            } elseif (\\is_array($object)) {\n                $message = \\sprintf('Impossible to invoke a method (\"%s\") on a sequence/mapping.', $item);\n            } else {\n                $message = \\sprintf('Impossible to invoke a method (\"%s\") on a %s variable (\"%s\").', $item, get_debug_type($object), $object);\n            }\n\n            throw new RuntimeError($message, $lineno, $source);\n        }\n\n        if ($object instanceof Template) {\n            throw new RuntimeError('Accessing \\Twig\\Template attributes is forbidden.', $lineno, $source);\n        }\n\n        // object property\n        if (Template::METHOD_CALL !== $type) {\n            if ($sandboxed) {\n                try {\n                    $env->getExtension(SandboxExtension::class)->getChecker()->checkPropertyAllowed($object, $item, $lineno, $source);\n                } catch (SecurityNotAllowedPropertyError $propertyNotAllowedError) {\n                    goto methodCheck;\n                }\n            }\n\n            static $propertyCheckers = [];\n","sourceCodeStart":1828,"sourceCodeEnd":1864,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1828-L1864","documentation":"Twig's CoreExtension::getAttribute() throws this RuntimeError when template code tries to call a method-like attribute ({{ var.item() }}) on a value that cannot have methods: an array is treated as a sequence/mapping, and other non-object values report their debug type. Twig aborts evaluation because method invocation is meaningless on that variable type.","triggerScenarios":"Calling a dynamic method on an array (e.g. {{ items.count() }} where items is a PHP array), or on a scalar/null in a template, while type is Template::METHOD_CALL and the value is not an object with that method.","commonSituations":"Upgrading from versions/configs where arrays silently returned null; passing arrays where objects (e.g. ArrayAccess, Collection) were expected; typos like {{ foo.keys() }} on arrays; strict_variables differences across environments.","solutions":["Fix the template to use the filter/function form for arrays (e.g. {{ items|length }}, {{ items|keys }}) instead of method calls","Convert the value passed to the template into an object (ArrayAccess or a class with the method)","Check the variable being passed from the controller/extension context; pass the intended object instead of an array","If the call is expected to be optional, guard with the 'defined' test before invoking"],"exampleFix":"// before (template)\n{{ products.count() }}\n\n// after\n{{ products|length }}","handlingStrategy":"type-guard","validationCode":"// in a Twig extension or before render\nif (!is_object($value) && !is_array($value)) { throw new \\InvalidArgumentException('Expected object for method call in template'); }","typeGuard":"function isMethodCallable($value, string $method): bool { return is_object($value) && method_exists($value, $method); }","tryCatchPattern":"try { $twig->render($tpl, $ctx); } catch (\\Twig\\Error\\RuntimeError $e) { /* inspect $e->getMessage() for 'Impossible to invoke a method' */ }","preventionTips":["Use filters/functions instead of method calls on arrays in templates","Keep strict_variables enabled in dev to catch these early","Type-hint template context data with value objects/DTOs"],"tags":["twig","runtime-error","method-call","array"],"backgroundTag":"unsupported-operation","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}