PHP判断{函数/类/方法}是否存在

本文介绍了PHP中如何使用内置函数来检测特定的类、函数及类方法是否存在。包括使用`function_exists`来检查函数是否定义,使用`class_exists`验证类是否存在,以及通过`method_exists`确认类的方法是否已定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)php判断系统函数或自己写的函数是否存在

bool function_exists ( string $function_name ) 判断函数是否已经定义,例如:

if(function_exists('curl_init')){
	curl_init();
}else{
	echo 'not function curl_init';
}

(2)php判断类是否存在

bool class_exists ( string $class_name [, bool $autoload = true ] ) 检查一个类是否已经定义,一定以返回true,否则返回false,例如:

if(class_exists('MySQL')){
    $myclass=new MySQL();
}

(3)php判断类里面的某个方法是否已经定义

bool method_exists ( mixed $object , string $method_name ) 检查类的方法是否存在,例如:

$directory=new Directory;
if(!method_exists($directory,'read')){
	echo '未定义read方法!';
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值