twigphp/Twig · error · RuntimeError
You cannot use the Twig function "constant" to access
Error message
You cannot use the Twig function "constant" to access "%s". You could provide an object and call constant("class", $object) or use the class name directly as a string. What it means
CoreExtension::constant() backs Twig's `constant()` function. When the requested constant is not defined and its name ends with `::class` (case-insensitive), Twig throws this specific RuntimeError because `Foo::class` is a compile-time PHP construct, not a runtime constant — `defined('Foo::class')` is always false. The error tells the developer to use the object form `constant('class', $object)` or pass the FQCN string directly.
Solutions
- Pass the object you have to the function: `constant('class', object)`
- If you only have the class name string, use it directly: `{{ 'App\\Entity\\User' }}` instead of `constant('App\\Entity\\User::class')`
- Use the `class` helper / `enum` function if available in your Twig version for class/enum resolution
- Wrap with `constant(..., checkDefined=true)` semantics (via `defined`) if the constant may legitimately be absent
Example fix
// before (Twig template)
{{ constant('App\\Entity\\User::class') }}
// after
{{ constant('class', user) }}
{{ constant('SOME_CONST', 'App\\Entity\\User'|enum) }} Defensive patterns
Strategy: type-guard
Validate before calling
// PHP, before rendering a template that uses constant('X::class')
if (is_object($value)) {
// use constant('class', $value) in the template
} else {
// pass the FQCN string directly to the template
} Type guard
function canUseConstant(string $name, ?object $object = null): bool
{
if (null !== $object) {
return 'class' === $name || defined($object::class.'::'.$name);
}
return defined($name) && '::class' !== strtolower(substr($name, -7));
} Try / catch
try {
$value = $twigExtensionConstant($name, $object);
} catch (\Twig\Error\RuntimeError $e) {
// ::class on a string class name: use the string itself or constant('class', $object)
$value = null;
} Prevention
- Never write `Foo::class` inside Twig constant(); pass objects or FQCN strings directly
- Prefer the `constant('class', $object)` form when an instance is in scope
- Search templates for the `::class` suffix as a lint rule
- Upgrade templates when migrating Twig versions that tightened constant() handling
When it happens
Trigger: Calling `{{ constant('Some\\Class::class') }}` in a template with only a string class name, or `constant('::class')` style usage, where the class exists but the `::class` pseudo-constant cannot be resolved via defined()/constant(). Not thrown when $checkDefined is true (returns false instead).
Common situations: Templates migrated from PHP-land habits where `Foo::class` works in code but not through constant(); trying to get a class name inside Twig from a string; dynamic class-name lookup in older templates after Twig tightened the check.
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
- Constant " " is undefined.
- The "format_list" filter requires the "IntlListFormatter"…
- SpanishInflector is not available.
- The " " filter is part of the , which is not…
- The " " function is part of the , which is not…
AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13).
Data as JSON: /api/errors/8961e74d79e15f26.
Report an issue: GitHub.
Appendix: source
Thrown at src/Extension/CoreExtension.php:1692
* @internal
*/
public static function constant($constant, $object = null, bool $checkDefined = false)
{
if (null !== $object) {
if ('class' === $constant) {
return $checkDefined ? true : $object::class;
}
$constant = $object::class.'::'.$constant;
}
if (!\defined($constant)) {
if ($checkDefined) {
return false;
}
if ('::class' === strtolower(substr($constant, -7))) {
throw new RuntimeError(\sprintf('You cannot use the Twig function "constant" to access "%s". You could provide an object and call constant("class", $object) or use the class name directly as a string.', $constant));
}
throw new RuntimeError(\sprintf('Constant "%s" is undefined.', $constant));
}
return $checkDefined ? true : \constant($constant);
}
/**
* Batches item.
*
* @param array $items An array of items
* @param int $size The size of the batch
* @param mixed $fill A value used to fill missing items
*
* @internal
*/
public static function batch($items, $size, $fill = null, $preserveKeys = true): arrayView on GitHub (pinned to a414c3a491)