ฟังก์ชัน get_defined_functions
ฟังก์ชัน get_defined_functions ส่งกลับอาร์เรย์หลายมิติที่มี
รายการฟังก์ชันทั้งหมดที่ถูกกำหนดไว้ อาร์เรย์ประกอบด้วยสองคีย์: 'internal' สำหรับฟังก์ชันภายใน
ของ PHP และ 'user' สำหรับฟังก์ชันที่ผู้ใช้กำหนด ฟังก์ชันนี้ไม่รับพารามิเตอร์
ไวยากรณ์
get_defined_functions();
ตัวอย่าง
ดึงรายการฟังก์ชันทั้งหมดที่ถูกกำหนดไว้:
<?php
function customFunction() {}
$res = get_defined_functions();
print_r(array_slice($res['internal'], 0, 3));
print_r($res['user']);
?>
ผลลัพธ์การทำงานของโค้ด (ตัวอย่าง):
[
'zend_version',
'func_num_args',
'func_get_args'
]
['customFunction']
ตัวอย่าง
ตรวจสอบการมีอยู่ของฟังก์ชันเฉพาะ:
<?php
$functions = get_defined_functions();
if (in_array('strpos', $functions['internal'])) {
echo 'Function strpos exists';
}
?>
ผลลัพธ์การทำงานของโค้ด:
'Function strpos exists'
ตัวอย่าง
นับจำนวนฟังก์ชันของผู้ใช้:
<?php
function func1() {}
function func2() {}
$res = get_defined_functions();
echo 'User functions count: ' . count($res['user']);
?>
ผลลัพธ์การทำงานของโค้ด:
'User functions count: 2'
ดูเพิ่มเติม
-
ฟังก์ชัน
function_exists,
ซึ่งตรวจสอบการมีอยู่ของฟังก์ชันเฉพาะ -
ฟังก์ชัน
get_defined_constants,
ซึ่งส่งกลับค่าคงที่ทั้งหมดที่ถูกกำหนดไว้