class_exists()

class_exists() — Checks if the class has been defined.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

This function checks whether or not the given class has been defined.

Signature

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

Parameters

ParameterTypeWhat it takes
$classstringThe class name. The name is matched in a case-insensitive manner.
$autoloadboolWhether to autoload if not already loaded.

Return value

Returns true if class is a defined class, false otherwise.

Example

A runnable snippet:

// Check that the class exists before trying to use it if (class_exists('MyClass')) { $myclass = new MyClass(); }

See also

function_exists(), enum_exists(), interface_exists(), get_declared_classes()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page class_exists() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.