PHP.nl

class_exists

class_exists

Checks if the class has been defined

bool **class_exists** string $class bool $autoload

This function checks whether or not the given class has been defined.

classThe class name. The name is matched in a case-insensitive manner.

autoload Whether to if not already loaded. autoload

Returns true if is a defined class, false otherwise. class

Voorbeeld: example

<?php
// Check that the class exists before trying to use it
if (class_exists('MyClass')) {
    $myclass = new MyClass();
}

?>

Voorbeeld: parameter example

<?php
spl_autoload_register(function ($class_name) {
    include $class_name . '.php';

    // Check to see whether the include declared the class
    if (!class_exists($class_name, false)) {
        throw new LogicException("Unable to load class: $class_name");
    }
});

if (class_exists(MyClass::class)) {
    $myclass = new MyClass();
}

?>

function_exists``enum_exists``interface_exists``get_declared_classes