{"record":{"id":"8961e74d79e15f26","repo":"twigphp/Twig","slug":"you-cannot-use-the-twig-function-constant-to-access-s-you","errorCode":null,"errorMessage":"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.","messagePattern":"You cannot use the Twig function \"constant\" to access \"(.+?)\"\\. You could provide an object and call constant\\(\"class\", \\$object\\) or use the class name directly as a string\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1692,"sourceCode":"     * @internal\n     */\n    public static function constant($constant, $object = null, bool $checkDefined = false)\n    {\n        if (null !== $object) {\n            if ('class' === $constant) {\n                return $checkDefined ? true : $object::class;\n            }\n\n            $constant = $object::class.'::'.$constant;\n        }\n\n        if (!\\defined($constant)) {\n            if ($checkDefined) {\n                return false;\n            }\n\n            if ('::class' === strtolower(substr($constant, -7))) {\n                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));\n            }\n\n            throw new RuntimeError(\\sprintf('Constant \"%s\" is undefined.', $constant));\n        }\n\n        return $checkDefined ? true : \\constant($constant);\n    }\n\n    /**\n     * Batches item.\n     *\n     * @param array $items An array of items\n     * @param int   $size  The size of the batch\n     * @param mixed $fill  A value used to fill missing items\n     *\n     * @internal\n     */\n    public static function batch($items, $size, $fill = null, $preserveKeys = true): array","sourceCodeStart":1674,"sourceCodeEnd":1710,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1674-L1710","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before (Twig template)\n{{ constant('App\\\\Entity\\\\User::class') }}\n// after\n{{ constant('class', user) }}\n{{ constant('SOME_CONST', 'App\\\\Entity\\\\User'|enum) }}","handlingStrategy":"type-guard","validationCode":"// PHP, before rendering a template that uses constant('X::class')\nif (is_object($value)) {\n    // use constant('class', $value) in the template\n} else {\n    // pass the FQCN string directly to the template\n}","typeGuard":"function canUseConstant(string $name, ?object $object = null): bool\n{\n    if (null !== $object) {\n        return 'class' === $name || defined($object::class.'::'.$name);\n    }\n    return defined($name) && '::class' !== strtolower(substr($name, -7));\n}","tryCatchPattern":"try {\n    $value = $twigExtensionConstant($name, $object);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    // ::class on a string class name: use the string itself or constant('class', $object)\n    $value = null;\n}","preventionTips":["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"],"tags":["php","twig","constant","class"],"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"}