Skip to content

defined("Class::CONST") answers false and constant("Class::CONST") fatals in the eval bridge #980

Description

@Guikingone

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:magicianTouches eval, include execution, or elephc-magician.bugSomething isn't workingtopic:php-compatChanges PHP compatibility or observable PHP semantics.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions