這篇文章主要介紹了php判斷類是否存在函數(shù)class_exists用法,實(shí)例分析了PHP針對類是否存在進(jìn)行判斷的應(yīng)用,對于自動(dòng)加載類以及類實(shí)例化之前的存在判斷來說都非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例分析了php判斷類是否存在函數(shù)class_exists用法。分享給大家供大家參考。具體如下:
如果我們要判斷一個(gè)類是不是可以用,可以先使用class_exists函數(shù)來判斷一下,下面來看幾個(gè)例子。
bool class_exists ( string $class_name [, bool $autoload = true ] )
此功能是否給定的類被定義檢查。this function checks whether or not the given class has been defined.
返回true,如果class_name是一個(gè)定義的類,否則返回false。
實(shí)例如下:
復(fù)制代碼代碼如下:
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();
}
希望本文所述對大家的PHP程序設(shè)計(jì)有所幫助。