作者:喜欢在他耳边唱歌 | 来源:互联网 | 2020-09-02 13:05
在PHP中可以使用“class_exists()”函数检测类是否存,该函数的作用是检查类是否已定义,其用法为“class_exists($class_name)”,其参数“$class_name”代表要检测的类名。
getMessage();
}
/**
* The above code either includes myfile.php or throws the new MyException
* as expected. No problem right? The same should be true of class_exists(),
* right? So then...
*/
$classname = 'NonExistentClass';
try {
if( ! class_exists( $classname ) ) {
throw new MyException('Double Doh!');
}
$var = new $classname();
}
catch( MyException $e ) {
echo $e->getMessage();
}
/**
* Should throw a new instance of MyException. But instead I get an
* uncaught LogicException blah blah blah for the default Exception
* class AND MyException. I only catch MyException so we've got on
* uncaught resulting in the dreaded LogicException error.
*/
?>
推荐教程:《PHP教程》
以上就是PHP检测类是否存在方法是什么?的详细内容,更多请关注 第一PHP社区 其它相关文章!