php判断类是否存在函数class_exists用法分析

yipeiwu_com5年前PHP代码库

本文实例分析了php判断类是否存在函数class_exists用法。分享给大家供大家参考。具体如下:

如果我们要判断一个类是不是可以用,可以先使用class_exists函数来判断一下,下面来看几个例子。

bool class_exists ( string $class_name [, bool $autoload = true ] )
此功能是否给定的类被定义检查。this function checks whether or not the given class has been defined.
返回true,如果class_name是一个定义的类,否则返回false。

实例如下:

复制代码 代码如下:
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程序设计有所帮助。

相关文章

php使用gettimeofday函数返回当前时间并存放在关联数组里

本文实例讲述了php使用gettimeofday函数返回当前时间并存放在关联数组里的方法。分享给大家供大家参考。具体分析如下: 英文官方描述如下: Key Descriptio...

PHP中fwrite与file_put_contents性能测试代码

function microtimeFloat() {    list($usec,$sec) = explode(" ", microtime());&n...

PHP 中英文混合排版中处理字符串常用的函数

# 判断某个位置是中文字符的左还是右半部分,或不是中文  # 返回值 -1 左 0 不是中文字符 1&nb...

解析file_get_contents模仿浏览器头(user_agent)获取数据

什么是user agentUser Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、...

PHPThumb PHP 图片缩略图库

1、这是一个缩略图类库 它是一个开源的图片缩略图类库,可以完成多种复杂的图片缩略图生成和现实,使用起来非常的方便。 2、使用方法 这里有一篇关于其简单实用的说明,英文比较简单,就不翻译了...