336 of 410 menu

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

get_declared_classes();

Example

Get a list of all declared classes in the script:

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

Code execution result:

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

Example

Add a custom class and look at the result:

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

Code execution result:

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

Example

Check for a specific class in the list:

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

Code execution result:

true

See Also

byenru