PHP interface_exists() Function
Last Updated :
03 Mar, 2023
The interface_exists() function is an inbuilt function in PHP that checks interface is defined or not.
Syntax:
bool interface_exists(string $interface, bool $autoload = true)
Parameters: This function accept two parameters that are described below:
- $interface: This parameter holds the interface name.
- $autoload: This parameter checks whether to call autoload or not by default.
Return Value: If the given interface is defined, then it returns “true”, otherwise it will return “false”.
Example 1: In this example, we will check interface is defined or not by using the interface_exists() function.
PHP
<?php
if ( interface_exists ( 'MyInterface' )) {
class MyClass implements MyInterface {
}
echo "A class using 'Interface' is created." ;
} else {
echo "'Interface' do not exist!." ;
}
?>
|
Output:
'Interface' does not exist!
Example 2: In the below code example, we will define an interface and then use the interface_exists() function.
PHP
<?php
interface MyInterface{
public function hello() ;
}
if ( interface_exists ( 'MyInterface' )) {
class MyClass implements MyInterface {
function hello(){
echo "Hey GeeksforGeeks" ;
}
}
echo "A class using 'Interface' is created.\n" ;
} else {
echo "'Interface' does not exist!." ;
}
$MyInterface = new MyClass() ;
$MyInterface ->hello() ;
?>
|
Output:
A class using 'Interface' is created.
Hey GeeksforGeeks
Reference: https://www.php.net/manual/en/function.interface-exists.php
Share your thoughts in the comments
Please Login to comment...