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

yipeiwu_com6年前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实现微信公众号支付功能图文详解

微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解

本文实例讲述了PHP实现微信公众号支付功能。分享给大家供大家参考,具体如下:    直言无讳,我就是一个初涉微信开发的小白,写这篇博客的原因:一是为了给自...

php+javascript的日历控件

复制代码 代码如下:<html> <head> <title>js calendar</title> <script languag...

自制PHP框架之设计模式

为什么要使用设计模式? 设计模式,我的理解是为了达到“可复用”这个目标,而设计的一套相互协作的类。 感兴趣的读者可以阅读《Design Patterns: Elements of Reu...

php简单生成随机颜色的方法

本文实例讲述了php简单生成随机颜色的方法。分享给大家供大家参考,具体如下: <?php //第一种方法: $rand = array('0', '1', '...

PHP批量生成图片缩略图的方法

本文实例讲述了PHP批量生成图片缩略图的方法。分享给大家供大家参考。具体如下: <?php //用PHP批量生成图片缩略图 function mkdirs($dirn...