日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 判断类存在,PHP怎么判断类是否存在

發布時間:2025/3/20 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 判断类存在,PHP怎么判断类是否存在 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PHP怎么判斷類是否存在

發布時間:2020-06-24 14:40:43

來源:億速云

閱讀:100

作者:Leah

PHP怎么判斷類是否存在?針對這個問題,這篇文章給出了相對應的分析和解答,希望能幫助更多想解決這個問題的朋友找到更加簡單易行的辦法。

在PHP中可以使用“class_exists()”函數檢測類是否存,該函數的作用是檢查類是否已定義,其用法為“class_exists($class_name)”,其參數“$class_name”代表要檢測的類名。

示例代碼<?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 判断类存在,PHP怎么判断类是否存在的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。