Note that this function also counts enums.
<?php
enum 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() 返回的顺序总是父类在前,子类在后。现在不会这样了。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.
<?php
enum 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 classone
class classone { }
//define classtwo
class classtwo { }
//This will show X classes (built-ins, extensions etc) with
//classone and classtwo as the last two elements
print_r(get_declared_classes());
//define classthree
class classthree { }
//...and four
class classfour { }
//Shows the same result as before with class three and four appended
print_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
)