PHP interface_exists() Function

What is PHP interface_exists() Function?

If you want to check whether a interface has been defined in your current script execution, use interface_exists() function.

Syntax:

interface_exists(interface_name, autoload)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

interface_name (Required): It specifies the interface name.

autoload (Optional): It determines whether to call the autoloader if the original interface is not found. It can take 2 values-

  • TRUE – it is the default value. It indicates that autoloader should be called if the original interface is not found.
  • FALSE – It indicates that autoloader should not be called if the original interface is not found.

Return Values:

The function returns-

  • TRUE if the interface name is defined.
  • FALSE if the interface name is not defined.

Examples:

Example 1:

<?php
interface LoggerInterface {}
$exist = interface_exists('LoggerInterface');
echo $exist ? "Interface LoggerInterface exists." : "Interface LoggerInterface not exists.";
?>

Output:

Interface LoggerInterface exists.

Example 2:

<?php
$exist = interface_exists('RenderableInterface');
echo $exist ? "Interface RenderableInterface exists." : "Interface RenderableInterface not exists.";
?>

Output:

Interface RenderableInterface not exists.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP interface_exists() Function

interface_exists() is a class/object type function in PHP. Use this function when you need tocheckthe existence of an interface.

Reference:

https://www.php.net/manual/en/function.interface-exists.php