get_declared_classes

(PHP 4, PHP 5, PHP 7, PHP 8)

get_declared_classes定義済のクラスの名前を配列として返す

説明

get_declared_classes(): array

定義済のクラスの名前を配列として返します。

パラメータ

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

戻り値

この関数は、現在のスクリプトで宣言されたクラスの名前の配列を返します。

注意:

PHP にコンパイル時に組み込んだり読み込んだりしている拡張モジュールの種類に依存して、 他のクラスも存在する可能性があることに注意しましょう。 これは、自作のクラスをそれらと同じ名前で作成できないことを意味します。 定義済みのクラスについては付録の定義済みクラスのセクションを 参照ください。

変更履歴

バージョン 説明
7.4.0 以前のバージョンでは、get_declared_classes() 関数は 子クラスの前に親クラスを常に返していました。 このバージョンからは、そうした動きはなくなりました。 この関数の戻り値について、特定の順序は保証されません。

例1 get_declared_classes() の例

<?php
print_r
(get_declared_classes());
?>

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

Array
(
    [0] => stdClass
    [1] => __PHP_Incomplete_Class
    [2] => Directory
)

参考

add a note

User Contributed Notes 2 notes

up
1
rmamdaminov at gmail dot com
1 year ago
Note that this function also counts enums.<?phpenum Bla{    case Foo;}var_dump(get_declared_classes());?>Result:array(116) {    ...    [115]=> string(3) "Bla"}
up
1
matt-php at DONT-SPAM-ME dot bitdifferent dot com
20 years ago
The array returned by this function will be in the order the classes were defined / included / required and this order does not appear to change.For example:<?PHP//define classoneclass classone { }//define classtwoclass classtwo { }//This will show X classes (built-ins, extensions etc) with//classone and classtwo as the last two elementsprint_r(get_declared_classes());//define classthreeclass classthree { }//...and fourclass classfour { }//Shows the same result as before with class three and four appendedprint_r(get_declared_classes());?>Output:Array(    [0] => stdClass   [1] .... other defined classes....   [10] => classone   [11] => classtwo )and...Array(    [0] => stdClass   [1] .... other defined classes....   [10] => classone   [11] => classtwo   [12] => classthree   [13] => classfour)
To Top