ReflectionProperty::getType

(PHP 7 >= 7.4.0, PHP 8)

ReflectionProperty::getTypeプロパティの型を取得する

説明

public ReflectionProperty::getType(): ?ReflectionType

プロパティに関連付けられた型を取得する

パラメータ

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

戻り値

プロパティが型を持っていれば、ReflectionType を返します。そうでなければ、 null を返します。

例1 ReflectionProperty::getType() の例

<?php
class User
{
public
string $name;
}

$rp = new ReflectionProperty('User', 'name');
echo
$rp->getType()->getName();
?>

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

string

参考

add a note

User Contributed Notes 1 note

up
6
email at dronov dot vg
5 years ago
class User{    /**     * @var string     */    public $name;}function getTypeNameFromAnnotation(string $className, string $propertyName): ?string{    $rp = new \ReflectionProperty($className, $propertyName);    if (preg_match('/@var\s+([^\s]+)/', $rp->getDocComment(), $matches)) {        return $matches[1];    }        return null;}    echo getTypeNameFromAnnotation('User', 'name');// string
To Top