PHP 8.3.4 Released!

ReflectionFunctionAbstract::getReturnType

(PHP 7, PHP 8)

ReflectionFunctionAbstract::getReturnType指定された関数の戻り値の型を取得する

説明

public ReflectionFunctionAbstract::getReturnType(): ?ReflectionType

指定されたリフレクションされた関数の戻り値の型を取得します。

パラメータ

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

戻り値

戻り値の型が指定されていた場合は ReflectionType オブジェクトを返します。 指定されていない場合は null を返します。

例1 ReflectionFunctionAbstract::getReturnType() の例

<?php

function to_int($param) : int {
return (int)
$param;
}

$reflection1 = new ReflectionFunction('to_int');
echo
$reflection1->getReturnType();

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

int

例2 ビルトイン関数で使う場合

<?php

$reflection2
= new ReflectionFunction('array_merge');

var_dump($reflection2->getReturnType());

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

null

こうなるのは、たくさんの内部関数が引数や戻り値で型を指定していないからです。 よって、このメソッドをビルトイン関数に使うのを避けるのが最適です。

参考

add a note

User Contributed Notes

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