get_declared_interfaces

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

Signature
get_declared_interfaces(): array
  • Added in PHP 5.0

Return value

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

get_declared_interfaces() is the interface-only sibling of get_declared_classes(). It returns the names of all interfaces the engine currently knows about — yours plus the built-ins that always exist, such as Traversable, Iterator, ArrayAccess, Countable, Stringable, JsonSerializable, UnitEnum and BackedEnum.

When to reach for it

The honest answer is: for tooling. Debug panels, architecture linters that check whether a contract has been loaded, and audits that snapshot the interface list before and after including a file are the realistic uses. Application code almost never needs the whole list.

Things that trip people up

  • Like every get_declared_* function, it reports what is loaded, not what exists. Under an autoloader, an interface no code has referenced yet will not be in the list.
  • To ask about one specific name, use interface_exists() instead of scanning this array — it also triggers autoloading, which the list does not.
  • To find out which interfaces a given class implements, use class_implements(); filtering the global list with is_subclass_of() is far more work for the same answer.
  • No ordering is guaranteed, and internal interfaces come first in practice, so sort before comparing output.

Example

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

When to use it

  • Snapshot the interface list before and after including a plugin file to see what it declared
  • Check in a debug panel which optional contracts an extension has registered
  • Verify at boot that every interface a config file names has actually been loaded

Frequently asked questions

Why does class_exists return false for an interface in this list?

Because class_exists() only reports classes. Interfaces have their own checker, interface_exists(), which also triggers the autoloader for a single name.

Does get_declared_interfaces include built-in interfaces?

Yes. Traversable, Iterator, ArrayAccess, Countable, Stringable, JsonSerializable, UnitEnum and others are always present, so the list is never empty.

How do I find which interfaces a class implements?

Use class_implements($objectOrClass), which returns them as an array keyed by name. Filtering get_declared_interfaces() answers a different question — what is loaded, not what a class uses.

More examples

Listing only your own interfaces

Interfaces are hoisted at compile time, so both names already exist when the snapshot runs — array_diff() therefore returns an empty array here, which is exactly the surprise worth knowing. Take the baseline in a separate file included before yours if you need a real diff.

Example · php
<?php
$builtIn = get_declared_interfaces();

interface Cacheable {}
interface Serialisable {}

$mine = array_values(array_diff(get_declared_interfaces(), $builtIn));
sort($mine);
print_r($mine);

Finding loaded implementations of a contract

class_implements() gives the interfaces of one class, which is the practical direction of the question. Note this scans declared classes, not declared interfaces — the interface list itself tells you nothing about who implements what.

Example · php
<?php
interface Notifier { public function send(string $m): string; }
class Email implements Notifier { public function send(string $m): string { return "email: $m"; } }
class Sms implements Notifier { public function send(string $m): string { return "sms: $m"; } }

foreach (get_declared_classes() as $c) {
    if (in_array(Notifier::class, class_implements($c) ?: [], true)) {
        echo $c, ' -> ', (new $c())->send('hi'), "\n";
    }
}

Discussion

  • Be the first to comment on this lesson.