Forum PHP 2025

ReflectionProperty::getSettableType

(PHP 8 >= 8.4.0)

ReflectionProperty::getSettableTypeset フックのパラメータの型を返す

説明

public ReflectionProperty::getSettableType(): ?ReflectionType

set フックに指定されたパラメータの型を返します。 set フックが定義されていない場合、 ReflectionProperty::getType() と同じ振る舞いをします。

パラメータ

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

戻り値

  • プロパティに set 可能な型にマッチする ReflectionType のインスタンスを返します。
  • 明示的に型を指定した set フックが存在する場合、 その型を返します。
  • フックが型を指定しない場合、またはフックが存在しない場合、 プロパティに指定された型を返します。 つまり、ReflectionProperty::getType() と同じ動きをします。 プロパティに型が指定されていない場合、null を返す可能性があります。
  • プロパティが仮想プロパティで、かつ set フックが存在しない場合、 never に対応する ReflectionType を返します。

例1 ReflectionProperty::getSettableType() の例

<?php

class Example
{
public
string $basic {
set => strtolower($value);
}

public
string $wider {
set(string|Stringable $value) => (string) $value;
}

public
string $virtual {
get => 'Do not change this';
}

public
$untyped = 'silly';
}

$rClass = new \ReflectionClass(Example::class);

var_dump($rClass->getProperty('basic')->getSettableType());
var_dump($rClass->getProperty('wider')->getSettableType());
var_dump($rClass->getProperty('virtual')->getSettableType());
var_dump($rClass->getProperty('untyped')->getSettableType());

?>

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

object(ReflectionNamedType)#3 (0) {
}
object(ReflectionUnionType)#2 (0) {
}
object(ReflectionNamedType)#3 (0) {
}
NULL

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top