All articles

Understand PHP execution and behavior

List Declared PHP Classes in a Namespace with get_declared_classes()

Filter declared PHP class names by namespace, print the matches, and check what the runtime list does and does not establish.

Editorial illustration for List Declared PHP Classes in a Namespace with get_declared_classes()

To list classes currently declared under a PHP namespace, call get_declared_classes() after loading the classes you care about, then filter its returned names by the namespace followed by a backslash. The result answers a runtime question, not a project-wide one. Because the supplied function documentation does not show the spelling of namespaced entries, the example below also lets you inspect the unfiltered names rather than asking you to take its expected output on faith.

Start with the declared class names

get_declared_classes() takes no arguments and returns an array. According to the PHP manual, that array contains the names of classes declared in the current script. The function has no namespace argument, so narrowing its result is a separate step.

Timing matters. Call it after the relevant classes have been declared or loaded. If a class is absent from the array at that point, the result does not establish whether a class file exists elsewhere in the project. The unfiltered array can also contain additional classes depending on which PHP extensions are compiled or loaded, so do not assume every entry belongs to your application.

Match the namespace, including its separator

This self-contained example declares two classes in Example\Inventory and one in the similarly named Example\InventoryArchive. It saves the returned array so you can inspect the names your runtime actually supplies, then prints those that pass the prefix check:

<?php
namespace Example\Inventory {
    class Item {}
    class Stockroom {}
}

namespace Example\InventoryArchive {
    class LegacyItem {}
}

namespace {
    $declared = get_declared_classes();
    $prefix = 'Example\\Inventory\\';
    $matches = [];

    foreach ($declared as $className) {
        if (strpos($className, $prefix) === 0) {
            $matches[] = $className;
        }
    }

    sort($matches); // Sort for display; the function guarantees no return order as of PHP 7.4.0.

    foreach ($matches as $className) {
        echo $className, PHP_EOL;
    }

    // For inspection, uncomment this line to see the unfiltered returned names:
    // var_dump($declared);
}

The intended filtered display is:

Example\Inventory\Item
Example\Inventory\Stockroom

That display depends on the returned entries having those namespace-qualified spellings. The supplied documentation establishes that the function returns declared class names, but it does not provide a namespaced return-value example. If you need to verify that detail in the PHP runtime you are inspecting, uncomment var_dump($declared) and compare its entries with the printed matches. This is a check to perform, not a claim that the example was run for this article.

The single-quoted PHP string 'Example\\Inventory\\' supplies the intended Example\Inventory\ prefix. The strpos(...) === 0 condition selects strings beginning with it. Comparing the strings makes the role of the final separator clear: Example\InventoryArchive\LegacyItem begins with Example\Inventory, but not with Example\Inventory\. Check the raw returned name if that distinction matters to your diagnosis.

Sorting changes presentation, not the prefix being tested. The PHP manual’s changelog says that, as of PHP 7.4.0, get_declared_classes() guarantees no particular return order. Sorting the matches makes the intended display predictable without relying on their positions in the original array.

Decide whether subnamespaces should count

The prefix test also accepts any returned name that begins with Example\Inventory\Tools\, because that string begins with Example\Inventory\. Thus a prefix filter treats matching subnamespace names as part of the branch. If you want only direct names, inspect the part after the prefix and exclude it when another backslash appears. Make that choice before interpreting the printed list.

Finally, read the result narrowly. Matches are names in the array returned during this execution. get_declared_classes() does not return a separate list of namespace names, and an absent match cannot establish that no corresponding class file exists. Load the classes of interest first, inspect the returned spelling where necessary, and then use the prefix filter to answer which returned names belong to the branch you chose.

← All articles