在PHP中如何检查类是否存在?

Des*_*est 2 php struct class

所以我在主类中有这种结构函数

function __construct(){
    $this->conf = $GLOBALS['conf'];
    $this->dbi = new dbinfo;
    $this->modOpt = new modOptions;
    $this->lang = new language;

    /** Connect DB extended Class **/
    parent::__construct($GLOBALS['connect']);
}
Run Code Online (Sandbox Code Playgroud)

我在其中定义类,但是这些类进入到库文件中,该文件包含在文件的开头,但其中一个除外,当请求后出现时将包括以下内容:

if (isset($_POST['delGroup']) && isset($_SESSION['content_viewer']) && $_SESSION['content_viewer']['code'] >= 1){   
    include_once(realpath(dirname(__FILE__) . '/../..')."/mod/dbinfo/proc.php");
}
Run Code Online (Sandbox Code Playgroud)

所以我想将检查添加到我的dbinfo类的构造函数中

function __construct(){
    $this->conf = $GLOBALS['conf'];
    if (isset(new dbinfo))
        $this->dbi = new dbinfo;

    $this->modOpt = new modOptions;
    $this->lang = new language;

    /** Connect DB extended Class **/
    parent::__construct($GLOBALS['connect']);
}
Run Code Online (Sandbox Code Playgroud)

但是此方法if isset无法正常工作,请向我展示如何检查类是否存在于文件中的正确方法。谢谢

Ray*_*Ray 5

尝试使用class_exists() http://php.net/manual/zh/function.class-exists.php

在您的情况下,寻找dbinfo班级可以这样做:

      if(class_exists('dbinfo')){
          //do something
Run Code Online (Sandbox Code Playgroud)

如果您的类具有命名空间,请包含ful命名空间的类名。


Jus*_*ons 5

class_exists('class_name',false);

设置falsetrue函数是否应该尝试加载类。