Longhorn PHP 2025

get_debug_type

(PHP 8)

get_debug_type変数の型名をデバッグしやすい形で取得する

説明

get_debug_type(mixed $value): string

PHP の変数 value の、解決済みの名前を返します。 この関数は、オブジェクトをクラス名に、 リソースをリソース型の名前に、 そしてスカラー型の値を型宣言で使われている通常の名前に解決します。

gettype() は、歴史的な理由で決まった型の名前を返します。 この関数は、それよりも実際に使われている型により近い名前を返すという点が異なります。

パラメータ

value

型をチェックする変数。

戻り値

返される文字列としてあり得るのは、以下になります:

型 + 状態 戻り値 備考
null "null" -
boolean (true または false) "bool" -
整数型 "int" -
浮動小数点型 "float" -
文字列 "string" -
配列 "array" -
リソース "resource (resourcename)" -
リソース(閉じられたもの) "resource (closed)" 例: fclose() で閉じられたファイルストリーム
名前付きのクラスのオブジェクト 名前空間も含めた、クラスの完全修飾名。 例: Foo\Bar -
無名クラスのオブジェクト "class@anonymous"。 クラスが別のクラスを継承したり、 インターフェイスを実装した場合は、親クラス/親のインターフェイス の名前。 例: "Foo\Bar@anonymous" 無名クラスのオブジェクトは、$x = new class { ... } という文法で作成されます。

例1 get_debug_type() の例

<?php

namespace Foo;

echo
get_debug_type(null), PHP_EOL;
echo
get_debug_type(true), PHP_EOL;
echo
get_debug_type(1), PHP_EOL;
echo
get_debug_type(0.1), PHP_EOL;
echo
get_debug_type("foo"), PHP_EOL;
echo
get_debug_type([]), PHP_EOL;

$fp = fopen('/examples/book.xml', 'rb');
echo
get_debug_type($fp), PHP_EOL;

fclose($fp);
echo
get_debug_type($fp), PHP_EOL;

echo
get_debug_type(new \stdClass), PHP_EOL;
echo
get_debug_type(new class {}), PHP_EOL;

interface
A {}
interface
B {}
class
C {}

echo
get_debug_type(new class implements A {}), PHP_EOL;
echo
get_debug_type(new class implements A,B {}), PHP_EOL;
echo
get_debug_type(new class extends C {}), PHP_EOL;
echo
get_debug_type(new class extends C implements A {}), PHP_EOL;

?>

上の例の出力は、 たとえば以下のようになります。

null
bool
int
float
string
array
resource (stream)
resource (closed)
stdClass
class@anonymous
Foo\A@anonymous
Foo\A@anonymous
Foo\C@anonymous
Foo\C@anonymous

参考

add a note

User Contributed Notes 1 note

up
3
vyacheslav dot belchuk at gmail dot com
1 year ago
Also, the function returns the correct type of Closure, as opposed to gettype()

<?php

echo get_debug_type(function () {}) . PHP_EOL;
echo
get_debug_type(fn () => '') . PHP_EOL . PHP_EOL;

echo
gettype(function () {}) . PHP_EOL;
echo
gettype(fn () => '');

?>

Output:

Closure
Closure

object
object
To Top