Skip to content
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

ReflectionProperty::getDefaultValue() and promoted properties #3386

Open
damianwadley opened this issue May 17, 2024 · 1 comment
Open

ReflectionProperty::getDefaultValue() and promoted properties #3386

damianwadley opened this issue May 17, 2024 · 1 comment

Comments

@damianwadley
Copy link
Member

Copied from https://bugs.php.net/bug.php?id=81386

Currently when you declare a promoted property with a default value the ReflectionProperty::getDefaultValue() won't return that value.

I'm not sure if this behavior is expected or not but in either case something needs to be changed.

If this behavior is unexpected then it should be fixed to return the correct value.

If this behavior is expected then the documentation of ReflectionProperty::getDefaultValue() should mention this drawback along with a few lines of code how to get to the relevant ReflectionParameter and get the value there.

<?php

class X {
    public function __construct(
        public bool $y = false
    ) {
    }
}

var_export((new ReflectionProperty('X', 'y'))->getDefaultValue());

Expected result: either the value should be returned or this behavior needs to be documented.

Actual result: the default value is not returned and there is no mention of this behavior in the documentation

@damianwadley
Copy link
Member Author

damianwadley commented May 17, 2024

As has been mentioned in some places, promoted properties do not have defaults. The defaults are applied to the constructor parameters - not the properties. Thus why there is an "extra" step of looking for property information from reflected constructor data (whose ReflectionParameters do indicate whether they're for promoted properties).

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

No branches or pull requests

2 participants