Friday, October 24, 2025
HomeLanguagesPHP interface_exists() Function

PHP interface_exists() Function

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 neveropen";
            }
        }
        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 neveropen

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS