PHP get_declared_interfaces() Function

What is PHP get_declared_interfaces() Function?

If you want to see all the interfaces used in your current script, use PHP get­_declared_interfaces() function. The output includes 2 types of classes-1) User defined interfaces and 2) built-in PHP interfaces.

Syntax:

get­_declared_interfaces()

Parameters:

The Function has no parameter.

Return Values:

This function returns an indexed array whose each element is an interface name. This function returns lots of built-in interfaces.

Examples:

Example:

<pre>
<?php
interface MyInterface {}
echo "This script has the following interfaces-" . "<br />";
print_r(get_declared_interfaces());
?>
</pre>

Output:

This script has the following interfaces-
Array
(
[0] => Traversable
[1] => IteratorAggregate
[2] => Iterator
[3] => Serializable
[4] => ArrayAccess
[5] => Countable
…..
[23] => DOMParentNode
[24] => DOMChildNode
[25] => MyInterface
)

Explanation:

The function includes lots of built-in interfaces including the last custom interface ”MyInterface”.

Notes on get_declared_interfaces() Function:

  • If you want to check whether a interface has been declared in your current script, use in_array() function in the output of this function. Check example below-
  • <?php
    interface MyInterface {}
    if(in_array('MyInterface', get_declared_interfaces())){
        echo "Interface 'MyInterface' declared.";
    }else{
        echo "Interface 'MyInterface' not declared.";
    }
    ?>
    

    Output:

    Interface 'MyInterface' declared.
  • The function doesn’t include interfaces from autoloader until it is instantiated or required explicitly.
  • Interface names are case-sensitive, meaning, the returned interface names are same as those were defined.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP get_declared_interfaces() Function

get_declared_interfaces() is a class/object type function in PHP. Use this function to see list of interfaces used in a script.

Reference:

https://www.php.net/manual/en/function.get-declared-interfaces.php