The class_exists Function
The class_exists
function checks if the specified class exists. The first parameter is the class name as a string, the second (optional) is a flag indicating whether to use autoloading.
Syntax
class_exists(string $class, bool $autoload = true): bool
Example
Let's check the existence of the standard class stdClass
:
<?php
$res = class_exists('stdClass');
var_dump($res);
?>
Code execution result:
true
Example
Let's check the existence of a non-existent class:
<?php
$res = class_exists('NonExistentClass');
var_dump($res);
?>
Code execution result:
false
Example
Let's check the existence of a class with autoloading disabled:
<?php
$res = class_exists('SomeClass', false);
var_dump($res);
?>
Code execution result:
false
See Also
-
the method_exists function,
which checks for the existence of a method -
the interface_exists function,
which checks for the existence of an interface -
the trait_exists function,
which checks for the existence of a trait -
the get_declared_classes function,
which returns all declared classes