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

phpclass_exists(string $class, bool $autoload = true): bool

Example

Let's check the existence of the standard class stdClass:

php<?php
  $res = class_exists('stdClass');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's check the existence of a non-existent class:

php<?php
  $res = class_exists('NonExistentClass');
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Let's check the existence of a class with autoloading disabled:

php<?php
  $res = class_exists('SomeClass', false);
  var_dump($res);
?>

Code execution result:

phpfalse

See Also