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() 返回的顺序总是父类在前,子类在后。现在不会这样了。get_declared_classes() 的返回值将不再保证顺序。

示例

示例 #1 get_declared_classes() 示例

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

以上示例的输出类似于:

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

参见

添加备注

用户贡献的备注 2 notes

up
1
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"
}
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 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
)
To Top