public static function ClassLoader::classExists

Checks whether a class with a given name exists. A class "exists" if it is either already defined in the current request or if there is an autoloader on the SPL autoload stack that is a) responsible for the class in question and b) is able to load a class file in which the class definition resides.

If the class is not already defined, each autoloader in the SPL autoload stack is asked whether it is able to tell if the class exists. If the autoloader is a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload function of the autoloader is invoked and expected to return a value that evaluates to TRUE if the class (file) exists. As soon as one autoloader reports that the class exists, TRUE is returned.

Note that, depending on what kinds of autoloaders are installed on the SPL autoload stack, the class (file) might already be loaded as a result of checking for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates these responsibilities.

Parameters

string $className The fully-qualified name of the class.:

Return value

boolean TRUE if the class exists as per the definition given above, FALSE otherwise.

1 call to ClassLoader::classExists()
ClassLoaderTest::testClassExists in drupal/core/vendor/doctrine/common/tests/Doctrine/Tests/Common/ClassLoaderTest.php

File

drupal/core/vendor/doctrine/common/lib/Doctrine/Common/ClassLoader.php, line 212

Class

ClassLoader
A <tt>ClassLoader</tt> is an autoloader for class files that can be installed on the SPL autoload stack. It is a class loader that either loads only classes of a specific namespace or all namespaces and it is suitable for working…

Namespace

Doctrine\Common

Code

public static function classExists($className) {
  if (class_exists($className, false) || interface_exists($className, false)) {
    return true;
  }
  foreach (spl_autoload_functions() as $loader) {
    if (is_array($loader)) {

      // array(???, ???)
      if (is_object($loader[0])) {
        if ($loader[0] instanceof ClassLoader) {

          // array($obj, 'methodName')
          if ($loader[0]
            ->canLoadClass($className)) {
            return true;
          }
        }
        else {
          if ($loader[0]
            ->{$loader[1]}($className)) {
            return true;
          }
        }
      }
      else {
        if ($loader[0]::$loader[1]($className)) {

          // array('ClassName', 'methodName')
          return true;
        }
      }
    }
    else {
      if ($loader instanceof \Closure) {

        // function($className) {..}
        if ($loader($className)) {
          return true;
        }
      }
      else {
        if (is_string($loader) && $loader($className)) {

          // "MyClass::loadClass"
          return true;
        }
      }
    }
  }
  return class_exists($className, false) || interface_exists($className, false);
}