php/doc-en

ReflectionProperty::getDefaultValue() and promoted properties

オープン

#3,386 opened on 2024/05/17

 (2 件のコメント) (0 件のリアクション) (0 人の担当者)XML (882 件のフォーク)auto 404
Extension: reflectiongood first issue

Repository metrics

Stars
 (596 個のスター)
PR merge metrics
 (平均マージ 99d 23h) (30d で 90 merged PRs)

説明

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

コントリビューターガイド