Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ฟังก์ชัน interface_exists() ใน PHP

คำอธิบาย

ฟังก์ชัน interface_exists() ตรวจสอบว่า interface ที่ระบุได้ถูกประกาศหรือยัง

รูปแบบการใช้งาน (Syntax)

interface_exists(interface_name, autoload)

พารามิเตอร์

  • interface_name — ชื่อ interface
  • autoload — เรียก __autoload หรือไม่ (ค่าเริ่มต้น true)

ค่าที่คืนกลับ

คืนค่า TRUE หาก interface มีอยู่ หรือ FALSE หากไม่มี

ตัวอย่างการใช้งาน

<?php
interface MyInterface {
    public function doSomething();
}

if (interface_exists('MyInterface')) {
    class MyClass implements MyInterface {
        public function doSomething() {
            echo "Doing something!";
        }
    }
    $obj = new MyClass();
    $obj->doSomething();
}
?>

ผลลัพธ์

Doing something!