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

220次阅读
没有评论

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 方法!';}

4.php 判断类里面的某个属性是否已经定义

bool property_exists (mixed $class , string $property ) 检查类的属性是否存在,例如:

$directory=new Directory;
if(!property_exists($directory,'li')){echo '未定义 li 属性!';}
正文完
有偿技术支持加微信
post-qrcode
 
评论(没有评论)
验证码