10

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

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Dest
  • 678
  • 2
  • 9
  • 27
  • http://php.net/manual/en/function.get-declared-classes.php ? – Marc B Jun 22 '16 at 15:14
  • 3
    Is there something ironic about the fact that PHP has a function actually called `class_exists()` ? http://php.net/manual/en/function.class-exists.php ... I can almost guarantee that had you just typed your question title into you'd have got this answer and saved yourself a tonne of time writing the question out. – CD001 Jun 22 '16 at 15:14
  • Duplicate: http://stackoverflow.com/questions/29940289/check-if-a-class-exist-in-a-file – Nicolas Bouvrette Jun 22 '16 at 15:15

2 Answers2

23

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.

Ray
  • 40,256
  • 21
  • 101
  • 138
5

class_exists('class_name',false);

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

JustOnUnderMillions
  • 3,741
  • 9
  • 12