What is the class_exists() method in PHP?
The class_exists() method can be used to check if the class is defined.
Syntax
class_exists(string $classname, bool $autoload = true): bool
Arguments
-
classname: The name of the class to be checked. -
autoload: A Boolean value denoting whether to call__autoloadby default. -
The
__autoloadwill try to load an undefined class.
Click here for more details.
Return type
This method returns true if the class is defined. Otherwise, false is returned.
Code
<?phpclass Test{ }echo "Checking if Test class exist : ";var_dump(class_exists('Test'));echo "Checking if Temp class exist : ";var_dump(class_exists('Temp'));?>
Explanation
In the code above, we have created a class with the name Test. We then called the class_exists method to check if the class exists.
We get true for the class name Test. We get false for the class name Temp, because the Temp class is not defined.