What is PHP get_declared_classes() Function?
If you want to see all the classes used in your current script, use PHP get_declared_classes() function. The output includes 2 types of classes-1) User defined classes and 2) built-in PHP classes.
Syntax:
get_declared_classes()
Parameters:
The Function has no parameter.
Return Values:
This function returns an indexed array whose each element is a class name. This function returns lots of built-in classes.
Examples:
Example 1:
<pre>
<?php
class Human {}
class Male {}
class Female {}
echo "This script includes the following classes-" . "<br />";
print_r(get_declared_classes());
class Tree {}
?>
</pre>
Output:
This script includes the following classes-
Array
(
[0] => InternalIterator
[1] => Exception
[2] => ErrorException
[3] => Error
[4] => CompileError
[5] => ParseError
[6] => TypeError
….
[171] => Human
[172] => Male
[173] => Female
[174] => Tree
)
Explanation:
The function includes lots of built-in classes including the first 6 ones.
Notes on get_declared_classes() Function:
- If you want to check whether a class has been declared in your current script, use in_array() function in the output of this function. Check example below-
<?php
class Human {}
class Male {}
class Female {}
if(in_array('Male',get_declared_classes())){
echo "Class 'Male' declared.";
}else{
echo "Class 'Male' not declared.";
}
?>
Output:
Class 'Male' declared.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP get_declared_classes() Function
get_declared_classes() is a class/object type function in PHP. Use this function to see list of classes used in a script.
Reference:
https://www.php.net/manual/en/function.get-declared-classes.php