The get_declared_classes Function

The get_declared_classes function returns an array containing the names of all classes that have been declared in the current script, including both user-defined classes and built-in PHP classes. The function does not accept parameters.

Syntax

phpget_declared_classes();

Example

Get a list of all declared classes in the script:

php<?php
  $res = get_declared_classes();
  print_r($res);
?>

Code execution result:

php['stdClass', 'Exception', 'Error', ...] // and other built-in PHP classes

Example

Add a custom class and look at the result:

php<?php
  class MyClass {}
  $res = get_declared_classes();
  print_r($res);
?>

Code execution result:

php['stdClass', 'Exception', 'Error', ..., 'MyClass']

Example

Check for a specific class in the list:

php<?php
  class TestClass {}
  $classes = get_declared_classes();
  $res = in_array('TestClass', $classes);
  var_dump($res);
?>

Code execution result:

phptrue

See Also