Skip to content

Language: final class constants (final const) — parse + override rules (zend_compile.c parity) #4455

Closed

Description

@PurHur

Category

language

Problem

PHP 8.1+ allows final const on classes/interfaces/traits/enums. Child classes cannot redefine or override a final const; Zend emits a compile-time error.

This compiler accepts ordinary const on classes (#2199) but does not model final on class constants — php-parser/php-cfg may parse the modifier without enforcing Zend inheritance rules.

php-src reference

  • Zend/zend_compile.c — zend_compile_const_decl, ZEND_ACC_CONSTANT + final bit
  • Zend/zend_inheritance.c — constant override checks
  • RFC: Final class constants (PHP 8.1)

Repro (today)

<?php
class Base {
    final const X = 1;
}
class Child extends Base {
    const X = 2;
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
php repro.php

Zend PHP: compile-time Error (cannot override final constant Base::X)
This compiler: may compile and run, printing 2 — incorrect.

Also verify final const on interfaces/traits and fetch semantics Child::X.

Scope (this repo)

Layer Files
Compiler lib/Compiler.php (compileClassConstDeclaration), lib/Compiler/ClassCompileRegistry.php
VM constant table + TYPE_CLASS_CONST_FETCH if runtime redefinition is possible
Tests test/compliance/cases/language/final_class_const.phpt

Done when

  • Child redefinition is compile-time fatal with Zend-compatible message.
  • Non-final constants still overridable per Zend rules.
  • ./script/ci-fast.sh --filter final_class_const green.

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 Jun 2, 2026
  2. PurHur commented on Jun 2, 2026

    @PurHur
    OwnerAuthor

    claim: cursor-worker-lane-c — starting this run

  3. PurHur commented on Jun 2, 2026

    @PurHur
    OwnerAuthor

    Merged via #4498

    Verification: Child::X cannot override final constant Base::X at compile time (issue repro + FinalClassConstCheckTest + VMTest --filter final_class_const). Non-final constants remain overridable (final_class_const_override_ok.phpt).

    php-src: Zend/zend_compile.c, Zend/zend_inheritance.c

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