在PHP中如何检查类是否存在? [英] In PHP how can i check if class exists?

查看:43
本文介绍了在PHP中如何检查类是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So i have this kind of struct function in main 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']);
}

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

where i define classes, but this classes is into library file which is included at the start of file except one, which is included when post request will appear like this:

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

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

so i want add check into my construct function for dbinfo class like this

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']);
}

,但是此方法与 if isset 一起使用时不起作用,请向我展示如何检查类是否存在于文件中的正确方法.谢谢

but this method with if isset does not works, please show me correct way how to check if class exists into file. thanks

推荐答案

尝试使用 class_exists() 在您的情况下,寻找 dbinfo 类可以做到这一点:

In your case, looking for dbinfo class do this:

      if(class_exists('dbinfo')){
          //do something

如果您的类具有名称空间,请包含ful名称空间的类名.

If your class has a namespace, include the ful namespaced classname.

这篇关于在PHP中如何检查类是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆