PHP 8.3.0 RC 6 available for testing

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
)

参见

add a note

User Contributed Notes 10 notes

up
1
Anonymous
18 years ago
Regarding note of 3-21:

<?php

class myclass {}

$class = 'myclass';
$instance = new $class();

?>

This function could also be used to determine the names of classes defined in a particular file by calling it before and after include. It's hardly a pointless function.
up
1
matt-php at DONT-SPAM-ME dot bitdifferent dot com
19 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
)
up
0
rmamdaminov at gmail dot com
24 days 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
-2
dexen + goofy _ pl
16 years ago
Summary:
* in PHP 5.1 class names have case preserved
* contrary, in PHP 4.4 class names are downcased, withe exception of a few build-in ones

The get_declared_classes() funcition returns the list of names with case preserved, as of PHP 5.1 series (prolly 5.0 too, but i have no way to test it right now). Since PHP generally is caseless in regard to names of classes, this may come at a surprise. Also, this could potentially break older code asssuming downcased list.

Take extra care when checking for existence of a class. Following example is, potentially, error prone: <?php in_array( $className, $classget_declared_classes() ) ?>

A sure-fire (while slower) way would be to iterate over the array and normalize case to, say, lower:

<?php
$exists
= FALSE;
$className = strtolower( $className );
foreach (
get_declared_classes() as $c ) {
if (
$className === strtolower( $c ) ) {
$exists = TRUE;
break;
}
}
?>

Optimization of the above snippet is left as a simple excercise to the reader ;)
-- dexen deVries
up
-4
brain at code dot com
9 years ago
get-declared-classes makes no sense at all, if u maybe, later for production, merge class files in one package file.

lets say: package.php
print_r(get_declared_classes());
class declaredHere { }
print_r(get_declared_classes());

so in this case, the declaredHerr class is defined at the first call of print_r();
because PHP-complier runs a hole file and declare Stuff before running the code.

But (Lovely PHP):
print_r(get_declared_classes());
if(true){
class declaredHere { }
}
print_r(get_declared_classes());
Will print the declaredHere class only in the second print_r.

Its not a Bug it a...
up
-3
smokey
20 years ago
you cannot remove them. they are "defined", which happens when the class is being loaded from the parser. you just deleted an instance of a class.
up
-3
matt at mattsoft dot net
17 years ago
classes can't be unloaded. probably not very practical to implement that in a future version. I wouldn't go out of my way to do it if I were zend. you're better off finding a workaround. it's better programming technique to find a way around having to do that anyway.

http://www.zend.com/zend/week/week223.php#Heading10
up
-5
dcahh at gmx de
15 years ago
This function considers only classes and subclasses. Not subsubclasses.

In fact I have code that provides an abstract class and then classes using this abstract class. Further I have subclasses to my concrete classes - which is why my subclasses are not listed within the returned array.
up
-6
Jazeps Basko
19 years ago
In PHP5, you don't get declared interfaces by calling this function!!!
To get interfaces you should use get_declared_interfaces(). However, to check if an interface is already defined, you should use class_exists()! This is strange, but PHP team does not think so.
up
-11
yarco dot wang at gmail dot com
9 years ago
those above comments are too old.
now, whatever the order is, the output will be the same:

<?php
class Test1
{}

print_r(get_declared_classes());

class
Test2
{}

print_r(get_declared_classes());

?>

will output the same result.
To Top