Skip to content

Stdlib: ReflectionProperty::getHooks() / setHook() — PHP 8.4 property hooks reflection (ext/reflection/php_reflection.c) #4806

Description

@PurHur

Category

stdlib · php-src-strict · spec refresh 2026-06-20

Problem

PHP 8.4 ReflectionProperty::getHooks(): array and setHook(string $name, Closure $hook) expose property hook callables for introspection. This compiler lowers hooks to __phpc_property_* methods (#3145) but ReflectionProperty has no getHooks / setHook builtins — method_exists($rp, 'getHooks') is false under bin/vm.php.

Frameworks (Symfony PropertyInfo, Laravel attribute scanners) need hook introspection separate from #4469 getValue/setValue and #4806-adjacent #10392 isset/empty semantics.

php-src reference

Repro

Commit test/repro/maintainer_reflection_property_gethooks.php:

<?php
declare(strict_types=1);

class Box {
    public string $label {
        get => strtoupper($this->label);
        set (string $v) { $this->label = $v; }
    }
    private string $label = 'hi';
}

$rp = new ReflectionProperty(Box::class, 'label');

echo method_exists($rp, 'getHooks') ? "getHooks yes\n" : "getHooks no\n";
echo method_exists($rp, 'setHook') ? "setHook yes\n" : "setHook no\n";

if (method_exists($rp, 'getHooks')) {
    $hooks = $rp->getHooks();
    ksort($hooks);
    echo implode(',', array_keys($hooks)), "\n";
}

$b = new Box();
echo $b->label, "\n";

if (method_exists($rp, 'setHook')) {
    $rp->setHook('get', fn() => 'OVERRIDE');
    echo $b->label, "\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/maintainer_reflection_property_gethooks.php
php bin/vm.php test/repro/maintainer_reflection_property_gethooks.php
'
Check Zend PHP 8.4+ VM today
method_exists(..., 'getHooks') true false ❌
method_exists(..., 'setHook') true false ❌
getHooks() keys get, set n/a
Read $b->label before override HI may work via hooks
After setHook('get', …) OVERRIDE n/a

Scope (PHP-in-PHP)

Area Path
VM builtins ext/standard/VmReflection.php — ReflectionProperty::getHooks, setHook
Registry lib/VM/BuiltinClasses.php — register methods on ReflectionProperty
Metadata Map hook callables from ClassProperty / staticPropertyHooks on ClassEntry
Tests test/compliance/cases/stdlib/reflection_property_hooks.phpt
Matrix script/capability-matrix.php
JIT/AOT VM v1; defer native lowering

Done when (php-src-strict)

  • Repro: both methods exist; getHooks() returns get + set entries
  • setHook('get', $closure) changes subsequent reads on live object
  • Invalid hook name → ValueError like Zend
  • ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter reflection_property_hooks' green
  • ./script/ci-fast.sh --filter reflection_property_hooks 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:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions