The class_exists()
method can be used to check if the class is defined.
class_exists(string $classname, bool $autoload = true): bool
classname
: The name of the class to be checked.
autoload
: A Boolean value denoting whether to call __autoload
by default.
The __autoload
will try to load an undefined class.
Click here for more details.
This method returns true
if the class is defined. Otherwise, false
is returned.
<?phpclass Test{ }echo "Checking if Test class exist : ";var_dump(class_exists('Test'));echo "Checking if Temp class exist : ";var_dump(class_exists('Temp'));?>
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.