Skip to content

Language: typed class constants — const array/string/int (PHP 8.3, Zend zend_constants.c parity) #3592

Description

@PurHur

Category

language

Problem

PHP 8.3+ allows typed class constants (public const array X = [1, 2];, const string NAME = 'x';). Zend validates types at compile time and stores values in the class constant table. This compiler accepts the syntax but defineClass() rejects non-scalar constant values even when the RHS is a literal array:

LogicException: Class constant value must be a compile-time constant

(VM stack: lib/VM.php:2415, May 2026).

Distinct from #3196 (object constants const X = new Foo();).

php-src reference

Repro (today)

<?php
class C {
    public const array X = [1, 2];
}
echo C::X[0];
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php /path/to/repro.php'

Expected (Zend): 1.
Actual: fatal Class constant value must be a compile-time constant during class registration.

Scope (this repo)

  • lib/Compiler.php — materialize literal array/string/int/float/bool/null const values at compile time
  • lib/VM.php — store typed constants; enforce type on fetch; C::X[$i] dim fetch on array constants
  • Type mismatch assignment at declare time → compile-time TypeError (Zend parity)
  • test/compliance/cases/language/typed_class_const.phpt
  • script/capability-syntax.php — document PHP 8.3 typed constants row

Done when

  • public const array X = [1,2]; and const string S = 'a'; work on VM; wrong-type value fails at compile time
  • echo C::X[0]; prints 1 in repro
  • JIT/AOT: either lower immutable constant arrays or document VM-only deferral in matrix

Activity

  1. added
    enhancementNew feature or request
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on May 30, 2026
  2. PurHur commented on May 30, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-b — starting this run

  3. PurHur commented on May 30, 2026

    @PurHur
    OwnerAuthor

    PR #3600: VM typed class constants (array fold + string) with compile-time type check. Verification: phpunit test/compliance/VMTest.php --filter typed_class_const. JIT array const lowering added; harness JIT class-const fetch still segfaults (pre-existing on class_member_const JIT cases).

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 / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-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