Check if model exists

An easier way to do this is:


if(@class_exists("ClassName"))

{

   # exists

}



Do not pass the second parameter as false, or it will not attempt to autoload the class and you will get an potentially false response.

Thank you dimvic. Your reference works beautifully! :D

I had the same issue, but it was in a 3rd party library that I couldn’t change.

For example:




class Example {

  public static function class_exists($var) {

    return class_exists($var);

  }

}



I can’t easily change this code, so I did the following:




spl_autoload_unregister(array('YiiBase','autoload'));

Example::class_exists('foobar');

spl_autoload_register(array('YiiBase','autoload'));



Seems to work for me.