PHP检测类能否存正在办法是甚么?
正在PHP中能够应用“class_exists()”函数检测类能否存,该函数的作用是反省类能否已界说,其用法为“class_exists($class_name)”,其参数“$class_name”代表要检测的类名。
保举视频教程:《PHP编程从入门到通晓(学习道路)》
示例代码
<?php // 应用前反省类能否存正在 if (class_exists('MyClass')) { $myclass = new MyClass(); } ?>
<?php /** * Set my include path here */ $include_path = array( '/include/this/dir', '/include/this/one/too' ); set_include_path( $include_path ); spl_autoload_register(); /** * Assuming I have my own custom exception handler (MyException) let's * try to see if a file exists. */ try { if( ! file_exists( 'myfile.php' ) ) { throw new MyException('Doh!'); } include( 'myfile.php' ); } catch( MyException $e ) { echo $e->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检测类能否存正在办法是甚么?的具体内容,更多请存眷资源魔其它相干文章!
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!