313 of 410 menu

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

byenru