Skip to content

ReflectionProperty::getRawValue() and related methods may call hooks of overridden properties #17713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arnaud-lb opened this issue Feb 6, 2025 · 4 comments

Comments

@arnaud-lb
Copy link
Member

Description

ReflectionProperty::getRawValue(), ::setRawValue(), ::setRawValueWithoutLazyInitialization() may call hooks when they reference a property of a parent class, that was overridden in the actual class, with hooks.

The following code:

class A {
    public $a = 'A::$a';
}
class B extends A {
    public $a = 'B::$a' {
        get { printf("Called hook: %s\n", __METHOD__); return $this->a; }
        set { printf("Called hook: %s\n", __METHOD__); $field = $value; }
    }
}

// Reference A::a
$r = new ReflectionProperty(A::class, 'a');

// Access B::a
var_dump($r->getRawValue(new B()));

Resulted in this output:

Called hook: B::$a::get
string(5) "B::$a"

But I expected this output instead:

string(5) "B::$a"

PHP Version

PHP 8.4

Operating System

No response

@iluuu1994
Copy link
Member

iluuu1994 commented Feb 6, 2025

Thank you! Small note: I think the set hook here has no effect. It also writes to $field, which I'm not sure is intentional (it used to refer to the property backing value in an old draft).

@arnaud-lb
Copy link
Member Author

You are right! The set hooks is a copy/paste from an old test, and has no effect.

@iluuu1994
Copy link
Member

@arnaud-lb Ah, I see! ext/reflection/tests/property_hooks/ReflectionProperty_getSetRawValue.phpt is wrong.

@arnaud-lb
Copy link
Member Author

This is the one I copied from :) But this is not really important as the hooks are not supposed to be called in this test or in the example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants