如果我們要判斷一個(gè)類是不是可以用,可以先使用class_exists函數(shù)來判斷一下,下面來看幾個(gè)例子.
bool class_exists ( string $class_name [, bool $autoload = true ] )
此功能是否給定的類被定義檢查,返回true,如果class_name是一個(gè)定義的類,否則返回false.
php實(shí)例代碼如下:
- if (class_exists('myclass')) {
- $myclass = new myclass();
- }
- function __autoload($class)
- {
- include($class . '.php');
- // check to see whether the include declared the class
- if (!class_exists($class, false)) {
- trigger_error("unable to load class: $class", e_user_warning);
- }
- }
- if (class_exists('myclass')) {
- $myclass = new myclass();
- }//開源代碼Vevb.com
新聞熱點(diǎn)
疑難解答