PHP: Hypertext Preprocessor (original) (raw)

class_implements

(PHP 5, PHP 7, PHP 8)

class_implements — Return the interfaces which are implemented by the given class or interface

Description

Parameters

object_or_class

An object (class instance) or a string (class or interface name).

autoload

Whether to autoload if not already loaded.

Return Values

An array on success, or [false](reserved.constants.php#constant.false) when the given class doesn't exist.

Examples

Example #1 class_implements() example

<?phpinterface foo { } class bar implements foo {}print_r(class_implements(new bar));// you may also specify the parameter as a string print_r(class_implements('bar'));spl_autoload_register();// use autoloading to load the 'not_loaded' class print_r(class_implements('not_loaded', true));?>

The above example will output something similar to:

Array ( [foo] => foo ) Array ( [foo] => foo ) Array ( [interface_of_not_loaded] => interface_of_not_loaded )

Notes

Note: To check that an object implements an interface, instanceof or the is_a() function should be used instead.

See Also

Found A Problem?

ludvig dot ericson at gmail dot nospam dot com

19 years ago

`Hint:

would check if 'your-interface' is ONE of the implemented interfaces.
Note that you can use something similar to be sure the class only implements that, (whyever you would want that?)

I use the first technique to check if a module has the correct interface implemented, or else it throws an exception.`

a dot panek at brainsware dot org

11 years ago

`Calling class_implements with a non-loadable class name or a non-object results in a warning:

This is not documented and should just return FALSE as the documentation above says.

`

trollll23 at yahoo dot com

19 years ago

`Luckily, it prints out superinterfaces as well in reverse order so iterative searching works fine:

prints out:

Array
(
[InterfaceB] => InterfaceB
[InterfaceA] => InterfaceA
)

`