Note that this function also counts enums.<?phpenum Bla{ case Foo;}var_dump(get_declared_classes());?>Result:array(116) { ... [115]=> string(3) "Bla"}
(PHP 4, PHP 5, PHP 7, PHP 8)
get_declared_classes — 定義済のクラスの名前を配列として返す
この関数にはパラメータはありません。
この関数は、現在のスクリプトで宣言されたクラスの名前の配列を返します。
注意:
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 )
Note that this function also counts enums.<?phpenum Bla{ case Foo;}var_dump(get_declared_classes());?>Result:array(116) { ... [115]=> string(3) "Bla"}
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)