當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP interface_exists()用法及代碼示例

interface_exists()function 是一個內置函數PHP檢查接口是否已定義。

用法:

bool interface_exists(string $interface, bool $autoload = true)

參數:該函數接受兩個參數,如下所述:

  • $interface:該參數保存接口名稱。
  • $autoload:該參數默認檢查是否調用自動加載。

返回值:如果給定的接口已定義,則返回“true”,否則返回“false”。

示例 1:在此示例中,我們將使用以下命令檢查接口是否已定義interface_exists()函數。

PHP


<?php
    if (interface_exists('MyInterface')) {
        class MyClass implements MyInterface {
    }
        echo "A class using 'Interface' is created.";
    } else {
        echo "'Interface' do not exist!.";
    }
?>

輸出:

'Interface' does not exist!

示例 2:在下麵的代碼示例中,我們將定義一個接口,然後使用interface_exists()函數。

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() ;
?>

輸出:

A class using 'Interface' is created.
Hey GeeksforGeeks

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



相關用法


注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 PHP interface_exists() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。