Php/docs/reflectionproperty.getdefaultvalue

提供:Dev Guides
< Php
移動先:案内検索

ReflectionProperty::getDefaultValue

(PHP 8)

ReflectionProperty::getDefaultValueプロパティで宣言されたデフォルト値を返す


説明

public ReflectionProperty::getDefaultValue ( ) : mixed

プロパティに明示または黙示的に宣言されているデフォルト値を取得します。


パラメータ

この関数にはパラメータはありません。


返り値

プロパティに何かしらデフォルト値が存在する場合(nullも含みます)、 それを返します。デフォルト値がない場合、null を返します。 デフォルト値が null の場合と、型付きプロパティが初期化されてない場合は区別できません。 それらを区別するには、ReflectionClass::hasDefaultValue() を使って下さい。


例1 ReflectionClass::getDefaultValue() の例

<?phpclass Foo {    public $bar = 1;    public ?int $baz;    public int $boing = 0;}$ro = new ReflectionClass(Foo::class);var_dump($ro->getProperty('bar')->getDefaultValue());var_dump($ro->getProperty('baz')->getDefaultValue());var_dump($ro->getProperty('boing')->getDefaultValue());?>

上の例の出力は以下となります。


int(1)
NULL
int(0)

参考