class_exists
(PHP 4, PHP 5, PHP 7, PHP 8)
class_exists — Checks if the class has been defined
Description
$class
, bool $autoload
= true
): boolThis function checks whether or not the given class has been defined.
Parameters
-
class
-
The class name. The name is matched in a case-insensitive manner.
-
autoload
-
Whether to call __autoload by default.
Return Values
Returns true
if class
is a defined class,
false
otherwise.
Examples
Example #1 class_exists() example
<?php
// Check that the class exists before trying to use it
if (class_exists('MyClass')) {
$myclass = new MyClass();
}
?>
Example #2 autoload
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();
}
?>
See Also
- function_exists() - Return true if the given function has been defined
- enum_exists() - Checks if the enum has been defined
- interface_exists() - Checks if the interface has been defined
- get_declared_classes() - Returns an array with the name of the defined classes