Skip to content

Language: readonly property with hooks — compile rejects valid PHP 8.4 pattern (Zend/zend_compile.c) #9835

Description

@PurHur

Category

language · php-src-strict · PHP 8.4 property hooks

Problem

PHP 8.4 allows readonly properties with set hooks initialized from the constructor (hook writes during object construction):

class C {
    public readonly int $x {
        get => $this->x;
        set { $this->x = $value; }
    }
    public function __construct() {
        $this->x = 0;
    }
}
$c = new C();
$c->x = 1; // runtime Error after init

This compiler rejects at compile:

Hooked properties cannot be readonly

Maintainer repro: test/repro/maintainer_gap_readonly_hook_write_exception.php.

php-src reference

Repro

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro/maintainer_gap_readonly_hook_write_exception.php
'
Engine Compile Runtime write after construct
Zend PHP 8.4+ OK catchable Error (not compile fail)
VM today compile fail n/a

Scope

  • lib/Compiler.php — allow readonly + hooks; enforce post-construct write rules at runtime
  • lib/VM.php — set hook + readonly interaction
  • test/compliance/cases/language/readonly_property_hook.phpt

Done when

  • Repro compiles on VM
  • Post-construct $c->x = 1 throws catchable Error matching Zend message class
  • Constructor init via set hook succeeds
  • ./script/ci-fast.sh --filter readonly_property_hook green

Related

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 / 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