【问题标题】:In PHP how can i check if class exists?在 PHP 中如何检查类是否存在?
【发布时间】:2016-06-22 15:10:56
【问题描述】:

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

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

我在哪里定义类,但是这些类是包含在文件开头的库文件中,除了一个,当发布请求显示如下时包含:

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

所以我想为这样的 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']);
}

但是if isset 的这种方法不起作用,请告诉我如何检查类是否存在于文件中的正确方法。谢谢

【问题讨论】:

标签: php class struct


【解决方案1】:

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

在你的情况下,寻找 dbinfo 类这样做:

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

如果您的类有命名空间,请包含完整的命名空间类名。

【讨论】:

    【解决方案2】:

    class_exists('class_name',false);

    如果函数应该尝试加载类,请将false 设置为true

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多