twigphp/Twig · error · RuntimeError
Impossible to invoke a method on a variable (dynamic…
Error message
Impossible to invoke a method on a variable (dynamic message).
What it means
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.
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
Example fix
// before (template)
{{ products.count() }}
// after
{{ products|length }} Defensive patterns
Strategy: type-guard
Validate before calling
// in a Twig extension or before render
if (!is_object($value) && !is_array($value)) { throw new \InvalidArgumentException('Expected object for method call in template'); } Type guard
function isMethodCallable($value, string $method): bool { return is_object($value) && method_exists($value, $method); } Try / catch
try { $twig->render($tpl, $ctx); } catch (\Twig\Error\RuntimeError $e) { /* inspect $e->getMessage() for 'Impossible to invoke a method' */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Failed to load Twig template
- The "cycle" function expects a non-empty sequence.
- The "random" function cannot pick from an empty sequence or…
- Regexp " " passed to "matches" failed: .
- Trimming side must be "left", "right" or "both".
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/52f32ee8083062b5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Extension/CoreExtension.php:1846
if (!\is_object($object)) {
if ($isDefinedTest) {
return false;
}
if ($ignoreStrictCheck || !$env->isStrictVariables()) {
return;
}
if (null === $object) {
$message = \sprintf('Impossible to invoke a method ("%s") on a null variable.', $item);
} elseif (\is_array($object)) {
$message = \sprintf('Impossible to invoke a method ("%s") on a sequence/mapping.', $item);
} else {
$message = \sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s").', $item, get_debug_type($object), $object);
}
throw new RuntimeError($message, $lineno, $source);
}
if ($object instanceof Template) {
throw new RuntimeError('Accessing \Twig\Template attributes is forbidden.', $lineno, $source);
}
// object property
if (Template::METHOD_CALL !== $type) {
if ($sandboxed) {
try {
$env->getExtension(SandboxExtension::class)->getChecker()->checkPropertyAllowed($object, $item, $lineno, $source);
} catch (SecurityNotAllowedPropertyError $propertyNotAllowedError) {
goto methodCheck;
}
}
static $propertyCheckers = [];
View on GitHub (pinned to a414c3a491)