defined("Class::CONST") answers false in the eval bridge, and constant("Class::CONST") fatals, where php resolves both.
Measured
<?php
class K { public const NAME = "k"; }
echo "defined_class_const:", (defined("K::NAME") ? "yes" : "no"), "\n";
echo "constant_class_const:", constant("K::NAME"), "\n";
php -n 8.5.6 → defined_class_const:yes then constant_class_const:k
- eval bridge →
defined_class_const:no, then Fatal error: eval() runtime failed: undefined constant "K::NAME"
Note the two halves fail differently: defined() is a SILENT wrong answer (a guard that should pass never does), constant() is a loud one.
Why it matters
Kernel::checkTrustedHeaders() style code guards with defined(Request::class."::HEADER_X") before configuring trusted headers, and throws InvalidArgumentException when the guard says no — so a false answer turns into a thrown exception several frames away from the cause. EventListener/DebugHandlersListener.php uses the same idiom.
A related case worth covering together, measured separately on this campaign: self::CONST and parent::CONST used inside a CLASS CONSTANT INITIALIZER fail before any code runs.
defined("Class::CONST")answers false in the eval bridge, andconstant("Class::CONST")fatals, where php resolves both.Measured
php -n8.5.6 →defined_class_const:yesthenconstant_class_const:kdefined_class_const:no, thenFatal error: eval() runtime failed: undefined constant "K::NAME"Note the two halves fail differently:
defined()is a SILENT wrong answer (a guard that should pass never does),constant()is a loud one.Why it matters
Kernel::checkTrustedHeaders()style code guards withdefined(Request::class."::HEADER_X")before configuring trusted headers, and throwsInvalidArgumentExceptionwhen the guard says no — so a false answer turns into a thrown exception several frames away from the cause.EventListener/DebugHandlersListener.phpuses the same idiom.A related case worth covering together, measured separately on this campaign:
self::CONSTandparent::CONSTused inside a CLASS CONSTANT INITIALIZER fail before any code runs.