Improve error message

It is not obvious that you cannot use the Twig function `constant` to get the special `::class` constants of classes like so: `{{ constant('Twig\\Extension\\CoreExtension::class') }}`, due to the underlying PHP function `\constant()`.

The previous error message in this case: 'Constant "Twig\Extension\CoreExtension::class" is undefined.' was not very helpful.
This commit is contained in:
Malte Wunsch
2023-06-19 19:53:08 +02:00
committed by Fabien Potencier
parent 4c621adb49
commit 3963851a41
+4
View File
@@ -1456,6 +1456,10 @@ final class CoreExtension extends AbstractExtension
}
if (!\defined($constant)) {
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));
}