Skip to content

Language: ?? on property hooks must not invoke get hook when backing is initialized (zend_property_hooks.c) #8902

Description

@PurHur

Category

language · php-src-strict

Problem

The null-coalescing operator ?? on a hooked property must check whether the property backing is initialized without calling the get hook (same rule as isset()). This compiler invokes the get hook before applying ??, so throwing getters emit a catchable/uncaught exception even when the backing value is present.

php-src reference

Repro

Save as test/repro/issue_property_hook_coalesce.php:

<?php
class C {
    public string $x {
        get { throw new Exception('get must not run for ??'); }
        set => $this->backing = $value;
    }
    private string $backing = 'a';
}
$c = new C();
var_dump($c->x ?? 'default');
echo "ok\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/issue_property_hook_coalesce.php
php bin/vm.php test/repro/issue_property_hook_coalesce.php
'
Runtime Output
Zend PHP 8.4+ string(1) "a" then ok
VM (wrong) Exception from get hook (may still print default after unwinding)

Also cover uninitialized backing: var_dump($c->x ?? 'default'); after unset($c->x) when unset is allowed — must return default without get hook.

Scope (PHP-in-PHP)

Layer Path
VM lib/VM.php — coalesce opcode on hooked properties; share probe with ??= (#6472)
Compiler Lowering unchanged
JIT/AOT Optional follow-up when hook coalesce appears in hot paths
Tests test/compliance/cases/language/property_hook_coalesce.phpt

Done when (php-src-strict)

Related

#8901 empty/isset · #6472 ??= backing check · #8870 foreach by-ref · #1492

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