Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP how can i check if class exists?

Tags:

php

class

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

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

like image 606
Dest Avatar asked Jun 22 '16 15:06

Dest


2 Answers

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.

like image 87
Ray Avatar answered Oct 17 '22 02:10

Ray


class_exists('class_name',false);

Set false to true if the function should try too load the class.

like image 32
JustOnUnderMillions Avatar answered Oct 17 '22 02:10

JustOnUnderMillions