PHP - Class/Object get_declared_interfaces() Function



The PHP Class/Object get_declared_interfaces() function is used to return an array listing the names of all interfaces created in the program. This function is useful for debugging and dynamic class handling.

Syntax

Below is the syntax of the PHP Class/Object get_declared_interfaces() function −

array get_declared_interfaces()

Parameters

This get_declared_interfaces() function has no parameters.

Return Value

This function returns an array of the names of the declared interfaces in the current script.

PHP Version

First introduced in core PHP 5 the get_declared_interfaces() function continues to function easily in PHP 7, and PHP 8.

Example 1

In the following example, we are using the PHP Class/Object get_declared_interfaces() function to get the array of built-in interfaces.

<?php
   print_r(get_declared_interfaces());
?> 

Output

Following is the output of the above code −

Array ( 
   [0] => Traversable
   [1] => IteratorAggregate
   [2] => Iterator
   [3] => ArrayAccess
   [4] => reflector
   [5] => RecursiveIterator
   [6] => SeekableIterator
)

Example 2

In this example, we will create one interface and then use the get_declared_interfaces() function to get a list of all declared interfaces.

<?php
   // Create interface here
   interface MyInterface {}

   // Get interfaces details
   $interfaces = get_declared_interfaces();

   // Display the result
   print_r($interfaces);
?>

Output

Here is the outcome of the following code −

Array
(
   [0] => Traversable
   [1] => IteratorAggregate
   [2] => Iterator
   [3] => Serializable
   [4] => ArrayAccess
   [5] => Countable
   [6] => Stringable
   [7] => Throwable
   [8] => UnitEnum
   [9] => BackedEnum
   . 
   . 
   .
   [27] => Ds\Sequence
   [28] => MyInterface
)

Example 3

In the below PHP code we will declare multiple inrerfaces and then use the get_declared_interfaces() function to see how it handles all the interfaces.

<?php
   // Create interfaces here
   interface FirstInterface {}
   interface SecondInterface {}
   
   // Get details of interfaces
   $interfaces = get_declared_interfaces();
   
   // Display the result
   print_r($interfaces);
?> 

Output

This will generate the below output −

Array
(
   [0] => Traversable
   [1] => IteratorAggregate
   [2] => Iterator
   [3] => Serializable
   [4] => ArrayAccess
   [5] => Countable
   [6] => Stringable
   [7] => Throwable
   [8] => UnitEnum
   [9] => BackedEnum
   . 
   . 
   .
   [27] => Ds\Sequence
   [28] => FirstInterface
   [29] => SecondInterface
)

Example 4

This example shows that even if a class implements an interface, get_declared_interfaces() will successfully list the interface.

<?php
   // Create interfaces here
   interface MyInterface {}
   
   // Create a class and implement the interface
   class MyClass implements MyInterface {}
   
   // Get details of interfaces
   $interfaces = get_declared_interfaces();
   
   // Display the result
   print_r($interfaces);
?> 

Output

This will create the below output −

Array
(
   [0] => Traversable
   [1] => IteratorAggregate
   [2] => Iterator
   [3] => Serializable
   [4] => ArrayAccess
   [5] => Countable
   [6] => Stringable
   [7] => Throwable
   [8] => UnitEnum
   [9] => BackedEnum
   . 
   . 
   .
   [27] => Ds\Sequence
   [28] => MyInterface
)
php_function_reference.htm
Advertisements