Skip to content

Language: circular class constants Error says Undefined constant — Zend “Cannot declare self-referencing constant” (zend_constants.c) #31837

Description

@PurHur

Category

Language: · php-src-strict · class constant evaluation

Problem

Mutual / self-referencing class constants must throw Error: Cannot declare self-referencing constant self::Y (Zend detects the cycle while declaring/evaluating). VM instead surfaces Error: Undefined constant A::X when the cycle is first read.

Probed 2026-08-17 — host Zend PHP 8.2 vs php bin/vm.php.

Repro Zend 8.2+ VM (2026-08-17)
class A { public const X = self::Y; public const Y = self::X; } then echo A::X Error: Cannot declare self-referencing constant self::Y Error: Undefined constant A::X
class A {
    public const X = self::Y;
    public const Y = self::X;
}
try {
    echo A::X;
} catch (Throwable $e) {
    echo $e->getMessage(), "\n";
}

php-src reference

PHP implementation target

  • lib/ class-constant evaluation / resolution — detect cycles and throw Zend’s self-referencing Error (do not report a one-sided “Undefined constant” when the peer name exists but is mid-evaluation)
  • php-in-PHP — no new C runtime branch

Repro

./script/docker-exec.sh -- bash -lc 'php test/repro/maintainer_gap_self_referencing_class_const.php'
./script/docker-exec.sh -- bash -lc 'php bin/vm.php test/repro/maintainer_gap_self_referencing_class_const.php'

Done when

  • Circular class consts throw Zend’s Cannot declare self-referencing constant … Error (message + that it is thrown on access/declare consistently with Zend)
  • Non-circular self::OTHER class consts still resolve
  • Compliance .phpt guard under test/compliance/cases/
  • php-src-strict; no php-compiler-strict shortcut

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:compilerCompiler / CFG / JITbugSomething isn't workingimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions