Skip to content

Language: child class const visibility narrowing accepted — Zend Access level fatal (Zend/zend_inheritance.c) #22929

Description

@PurHur

Category

bug · php-src-strict · language / inheritance

Problem

Zend forbids narrowing class constant visibility on inheritance (public constprivate const in a child). php-compiler accepts the composition (prints compiled, exit 0). Accessing B::X later fails at runtime (Cannot access private constant) instead of the Zend compile fatal.

Repro Zend 8.2+ (2026-07-24) VM (2026-07-24)
class A { public const X = 1; } class B extends A { private const X = 2; } echo "compiled\n"; Fatal: Access level to B::X must be public (as in class A) (exit 255) compiled (exit 0)

php-src reference

PHP implementation target

  • lib/ inheritance / class-const composition check — when overriding an inherited class const, require visibility ≥ parent (same Zend message)
  • Prefer PHP compile-time guard; no new runtime/*.c

Repro

./script/docker-exec.sh -- bash -lc 'cat >/tmp/const_vis.php <<'"'"'PHP'"'"'
<?php
class A { public const X = 1; }
class B extends A { private const X = 2; }
echo "compiled\n";
PHP
php /tmp/const_vis.php; echo zend:$?
php bin/vm.php /tmp/const_vis.php; echo vm:$?'

Done when

  • Snippet compile-fatals on VM/JIT/AOT with Zend-shaped Access level to B::X must be public (as in class A)
  • Widening / same visibility still allowed
  • Compliance .phpt under test/compliance/cases/language/
  • php-src-strict; no php-compiler-strict shortcut

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