The class_implements Function

The class_implements function returns an array of interface names that are implemented by the specified class or interface. The first parameter takes an object or class name as a string, and the second optional parameter determines whether autoloading should be used.

Syntax

phpclass_implements(
  object|string $class,
  [bool $autoload = true]
): array|false

Example

Let's get interfaces for the built-in Exception class:

php<?php
  $res = class_implements('Exception');
  print_r($res);
?>

Code execution result:

php['Throwable']

Example

Let's create a custom interface and class, then check the implemented interfaces:

php<?php
  interface MyInterface {}
  class MyClass implements MyInterface {}
  
  $res = class_implements('MyClass');
  print_r($res);
?>

Code execution result:

php['MyInterface']

Example

Let's check interfaces for the interface itself:

php<?php
  interface ParentInterface {}
  interface ChildInterface extends ParentInterface {}
  
  $res = class_implements('ChildInterface');
  print_r($res);
?>

Code execution result:

php['ParentInterface']

See Also

  • the class_parents function,
    which returns parent classes
  • the class_uses function,
    which returns traits used by a class