Dest Dest - 5 months ago 47
PHP Question

In PHP how can i check if class exists?

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");
}


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


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

Ray Ray
Answer

Try using, class_exists() http://php.net/manual/en/function.class-exists.php

In your case, looking for dbinfo class do this:

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

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