Check if a PHP class exists
We've discussed how to check if a class has a function but what if the class doesn't exist at all? To check if a class exists you can use, use guessed it, class_exists().
if (class_exists('MyClass'))
{
$object = new MyClass();
}
else
{
throw new Exception('The class MyClass does not exist.');
}
As of PHP 5.0.0 the class_exists() function will attempt to use __autoload() to load the class if available. This default behavior can be overwritten by passing a second parameter of false to class_exists().
Also worth nothing, as of PHP 5.0.2 class_exists() will return false if you pass it an interface. When checking if an interface exists, you can use interface_exists().