{"record":{"id":"985161ee3792276d","repo":"twigphp/Twig","slug":"constant-s-is-undefined","errorCode":null,"errorMessage":"Constant \"%s\" is undefined.","messagePattern":"Constant \"(.+?)\" is undefined\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1695,"sourceCode":"    {\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\n    {\n        if (!is_iterable($items)) {\n            throw new RuntimeError(\\sprintf('The \"batch\" filter expects a sequence or a mapping, got \"%s\".', get_debug_type($items)));","sourceCodeStart":1677,"sourceCodeEnd":1713,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1677-L1713","documentation":"CoreExtension::constant() throws this RuntimeError when Twig's `constant()` function is called with a constant name that is not defined via defined(), and the name is not the special `::class` case. This mirrors PHP's own \"Undefined constant\" error but as a Twig RuntimeError, so templates fail with a clear message instead of a PHP fatal Error.","triggerScenarios":"`{{ constant('FOO_BAR') }}` when FOO_BAR is not defined; `{{ constant('MISSING', object) }}` when the object's class lacks that constant; typos in constant names; constants removed from a dependency after a version upgrade; referencing constants with wrong casing/namespace in the template string.","commonSituations":"Upgrading a library that removed or renamed a class constant while templates still reference it; typo in the constant name inside the template; the constant exists only in a different namespace than the string passed; constants that only exist in dev/test environments.","solutions":["Correct the constant name/namespace in the template to match the actual defined constant","Verify with php -r 'var_dump(defined(\"FOO_BAR\"));' or grep the class for the constant","Guard in the template with `{% if constant('FOO_BAR') is defined %}`... wait — use `{{ FOO_BAR is defined ? constant('FOO_BAR') : default }}` pattern via defined checks where supported","If the constant was removed upstream, replace it with the new name or inline the value"],"exampleFix":"// before (Twig template)\n{{ constant('User::ROLE_ADMIN') }}\n// after (constant lives on App\\Entity\\User and an object is in scope)\n{{ constant('ROLE_ADMIN', user) }}","handlingStrategy":"validation","validationCode":"// PHP, before rendering\nif (!\\defined('App\\\\Constants::ROLE_ADMIN')) {\n    throw new \\LogicException('Referenced constant no longer exists; update templates.');\n}","typeGuard":"function constantExists(string $name, ?object $object = null): bool\n{\n    return null !== $object\n        ? ('class' === $name || \\defined($object::class.'::'.$name))\n        : \\defined($name);\n}","tryCatchPattern":"try {\n    $value = \\Twig\\Extension\\CoreExtension::constant($name, $object);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    if (!str_starts_with($e->getMessage(), 'Constant \"') ) { throw $e; }\n    $value = $default;\n}","preventionTips":["Keep a central PHP constants class and reference its values in templates only via tested names","Run template smoke tests in CI that render pages using constant()","Grep templates when upgrading dependencies that may rename/remove constants","Use `is defined` checks in templates for optional constants"],"tags":["php","twig","constant","undefined"],"backgroundTag":"missing-config-value","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"}