Skip to content

Language: ??= on property hooks must invoke get/set hooks (zend_property_hooks.c) #6472

Description

@PurHur

Category

language

Problem

Null-coalesce assignment (??=) on a property hook must read via the get hook and write via the set hook when the backing value is unset/null — same as plain $obj->prop = … assignment in php-src.

This compiler treats ??= like a raw property write: VM emits "Attempt to read property … on null" warnings and never invokes hooks.

Distinct from #6427 (compound +=/.= operators) — ??= uses coalesce+assign opcodes (lib/Compiler.php coalesce paths).

php-src reference

Repro

<?php
class C {
    private ?string $x = null;
    public string $y {
        get => $this->x ?? 'default';
        set => $this->x = $value;
    }
}
$c = new C();
$c->y ??= 'assigned';
echo $c->y, "\n";
// Zend: assigned
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
cat > /tmp/hook_nullcoalesce.php <<'"'"'PHPEOF'"'"'
<?php
class C {
    private ?string $x = null;
    public string $y { get => $this->x ?? "default"; set => $this->x = $value; }
}
$c = new C();
$c->y ??= "assigned";
echo $c->y, "\n";
PHPEOF
php /tmp/hook_nullcoalesce.php
php bin/vm.php /tmp/hook_nullcoalesce.php
'

Today: VM prints warnings and wrong output (not assigned).

Scope

Path Work
lib/Compiler.php compileCoalesce / assign paths — hook-aware dim fetch for ??= lvalues
lib/VM.php Coalesce left branch must call get hook; right branch must call set hook
lib/JIT.php Mirror when property has lowered hooks

Done when

  • Repro prints assigned on VM without warnings
  • When backing is already set, ??= is a no-op (get hook value preserved)
  • test/compliance/cases/language/property_hook_nullcoalesce_assign.phpt green
  • JIT/AOT pass when script contains only hook-safe opcodes

Strictness

php-src-strict

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