get_declared_classes
(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 )
参见
- class_exists() - 查类是否已经定义
- get_declared_interfaces() - 返回一个数组包含所有已声明的接口
- get_defined_functions() - 返回所有已定义函数的数组
+添加备注
用户贡献的备注 2 notes
rmamdaminov at gmail dot com ¶
1 year ago
Note that this function also counts enums.
<?php
enum Bla
{
case Foo;
}
var_dump(get_declared_classes());
?>
Result:
array(116) {
...
[115]=> string(3) "Bla"
}
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 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
)
备份地址:http://www.lvesu.com/blog/php/function.get-declared-classes.php