Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP class_exists() and related functions

Reference for class_exists(), interface_exists(), trait_exists(), enum_exists(), get_parent_class(), class_implements() and is_a() with examples.

PHP can answer questions about classes while the program runs: does a class exist, what is its parent, which interfaces does it implement? These checks are useful for plugin systems, factories that build objects from names, and defensive code.

The existence functions come in one flavor per kind of type: class_exists(), interface_exists(), trait_exists() and enum_exists(). Each takes the name and an optional flag saying whether the autoloader may run.

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

Try it

PHP Does it exist, and what is it?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Parents, interfaces and traits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class_exists(string $class, bool $autoload = true): bool
interface_exists(string $interface, bool $autoload = true): bool
trait_exists(string $trait, bool $autoload = true): bool
enum_exists(string $enum, bool $autoload = true): bool
get_parent_class(object|string $objectOrClass = ?): string|false
class_implements(object|string $objectOrClass, bool $autoload = true): array|false
class_parents(object|string $objectOrClass, bool $autoload = true): array|false
class_uses(object|string $objectOrClass, bool $autoload = true): array|false
is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool
is_subclass_of(mixed $object_or_class, string $class, bool $allow_string = true): bool

Return values: the existence functions return bool. The class_* functions return an array whose keys and values are the names, or false (with a warning) if the class is unknown. get_parent_class() returns false when there is no parent.

Usage notes

  • Names are case-insensitive (class_exists('circle') is true) and should be written without a leading backslash but with the full namespace.
  • Kinds are separate. class_exists() is false for interfaces and traits. Enums count as classes, so class_exists() is true for them too; use enum_exists() to tell.
  • Autoloading. By default the autoloader runs, which can load files. Pass false to check only what is already declared.
  • is_a() versus is_subclass_of(). is_a() is true for the class itself; is_subclass_of() is not. is_a() accepts a class name string only if $allow_string is true.
  • instanceof is usually clearer when the class name is known at write time; use these functions for names held in variables.
  • Validate names from input before instantiating (new $name) and compare against an allow-list.
  • Introspection of members is in method_exists() and Reflection.