1.function_exists — 如果给定的函数已经被定义就返回TRUE
参数
-
函数名,必须为一个字符串。
function_name
返回值
如果 function_name 存在且的确是一个函数就返回 TRUE ,反之则返回 FALSE 。
Note:
对于语法结构的判断,例如 include_once 和echo 将会返回
FALSE。范例
Example #1 function_exists() 的例子
<?php
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br />\n";
} else {
echo "IMAP functions are not available.<br />\n";
}
?>
参数
-
对象示例或者类名。
-
方法名。
object
method_name
返回值
如果 method_name 所指的方法在 object 所指的对象类中已定义,则返回 TRUE,否则返回 FALSE。
Example #1 method_exists() 例子
<?php
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
?>
以上例程会输出:
bool(true)
Example #2 Static method_exists() 例子
<?php
var_dump(method_exists('Directory','read'));
?>
以上例程会输出:
bool(true)
本文介绍了PHP中的两个重要函数:function_exists用于检查指定函数是否已定义,method_exists用于检查类中是否存在特定方法。通过示例展示了如何使用这两个函数来增强代码的健壮性和灵活性。
491

被折叠的 条评论
为什么被折叠?



