get_declared_classes

Returns the names of every class currently defined in the running script.

Signature
get_declared_classes(): array
  • Added in PHP 4.0

Return value

arrayReturns an array of the names of the declared classes in the current script.

Changelog

  • PHP 7.4.0Parent classes are no longer guaranteed to appear before their children. No particular order is guaranteed for the return value.

get_declared_classes() returns every class name known to the engine at the moment you call it: your classes, the classes of every loaded extension, and internal classes such as Exception and ArrayObject. Interfaces and traits are not included; enums are, because an enum is a class.

The autoloading catch

The list reflects what has actually been loaded, not what exists on disk. In an autoloaded application most of your classes have never been touched, so they are absent. Calling this function twice at different points in a request will give different answers. That makes it useless as a class discovery mechanism — for that, scan the filesystem or read Composer's class map.

When to reach for it

It earns its place in tooling and diagnostics: dumping the loaded set in a debug panel, or capturing a snapshot before and after including a file to see exactly what that file declared. Combined with is_subclass_of() it can find every already-loaded implementation of a base class, which is fine for plugins that were explicitly required earlier in the boot.

Expect several hundred entries on a bare CLI and thousands in a framework, so always filter before printing.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Snapshot the class list before and after an include to see what a file declared
  • Find every already-loaded subclass of a plugin base class during boot
  • Show the loaded class count in a debug or profiling panel

Frequently asked questions

Does get_declared_classes include classes that have not been autoloaded yet?

No. It only lists classes the engine has already defined, so under an autoloader your unreferenced classes are missing. Use a filesystem scan or Composer's class map for discovery.

Are interfaces and traits included in get_declared_classes?

No. Use get_declared_interfaces() and get_declared_traits() for those. Enums do appear, because PHP implements them as classes.

Is the order of get_declared_classes reliable?

No. Since PHP 7.4 no ordering is guaranteed — parents may appear after their children — so sort the array yourself if you need stable output.

More examples

Seeing what an include declared

array_diff() against the earlier snapshot isolates exactly what was added. The same before/after pattern works with require to audit a plugin file, with eval() standing in here only because the example must be self-contained.

Example · php
<?php
$before = get_declared_classes();

eval('class RuntimeWidget { public $id = 1; }');

$new = array_values(array_diff(get_declared_classes(), $before));
print_r($new);
echo "declared ", count($new), " new class(es)\n";

Finding loaded subclasses of a base

is_subclass_of() filters the full list down to concrete drivers and excludes the abstract Driver itself. This only finds drivers whose files have already been loaded, which is why plugin systems usually require them explicitly first.

Example · php
<?php
abstract class Driver { abstract public function name(): string; }
class MysqlDriver extends Driver { public function name(): string { return 'mysql'; } }
class SqliteDriver extends Driver { public function name(): string { return 'sqlite'; } }

foreach (get_declared_classes() as $c) {
    if (is_subclass_of($c, Driver::class)) {
        echo $c, ' -> ', (new $c())->name(), "\n";
    }
}

Discussion

  • Be the first to comment on this lesson.