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

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

คำอธิบาย

ฟังก์ชัน method_exists() ตรวจสอบว่าคลาสหรือออบเจ็กต์มีเมธอด (method) ที่ระบุหรือไม่

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

method_exists(object|class, method_name)

พารามิเตอร์

  • object|class — ออบเจ็กต์หรือชื่อคลาส
  • method_name — ชื่อเมธอดที่ต้องการตรวจสอบ

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

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

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

<?php
$directory = new Directory('.');
var_dump(method_exists($directory, 'read'));      // true
var_dump(method_exists($directory, 'anything'));  // false

// ตรวจสอบจากชื่อคลาส
var_dump(method_exists('Directory', 'read'));     // true
?>

ผลลัพธ์

bool(true)
bool(false)
bool(true)