Thursday, July 23, 2026
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

2 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS