Get a list of all available classes with PHP
Posted July 10th, 2008 in PHP
PHP has the function get_declared_classes() which allows you to get a list of all the available system and user defined classes in an array. This post looks at how to use the get_declared_classes() function, the output from it and how to sort the list into alphabetical order.
Using get_declared_classes()
get_declared_classes() returns a zero based array containing all the classes available to your script, including both system declared classes (such as PDO and XMLReader) and the classes you have declared yourself or from any 3rd party libraries you have included in your script.
The following example illustrates this (the examples below assume we have two user defined classed called foo and bar):
print_r(get_declared_classes());
and an extract of the result:
Array
(
[0] => stdClass
[1] => Exception
[2] => ErrorException
[3] => LibXMLError
[4] => __PHP_Incomplete_Class
[5] => php_user_filter
...
[100] => XMLWriter
[101] => XSLTProcessor
[102] => foo
[103] => bar
)
Sorting the result into alphabetical order
If you want to sort the list of classes into alphabetical order, you could do this instead:
$classes = get_declared_classes(); sort($classes); print_r($classes);
and an extract of the result:
Array
(
[0] => AppendIterator
[1] => ArrayIterator
[2] => ArrayObject
[3] => BadFunctionCallException
[4] => BadMethodCallException
[5] => CachingIterator
...
[100] => mysqli_stmt
[101] => mysqli_warning
[102] => php_user_filter
[103] => stdClass
)
Conclusion
The get_declaed_functions() function is a great way of getting a complete list of classes that are available on your install of PHP and from your own and 3rd party PHP libraries.
Related posts:
- Get the class name and parent class name in PHP (Wednesday, November 19th 2008)
- Get a list of all available constants with PHP (Thursday, July 17th 2008)
- Get a list of all available functions with PHP (Thursday, July 3rd 2008)
Recent posts:
- List installed packages with YUM (Tuesday, December 2nd 2008)
- Monthly Roundup - November 2008 (Monday, December 1st 2008)
- Weekly Roundup - December 1st 2008 (Monday, December 1st 2008)
- Installing subversion on CentOS (Sunday, November 30th 2008)
- GoDaddy 99 cent .com domain coupon code (Saturday, November 29th 2008)
- Find the index of a string within a string with Javascript (Friday, November 28th 2008)
Subscribe to RSS Feed / Email / Bookmark / Share
Use the buttons below to subscribe to my RSS feed to be notified next time something is posted, share this post with others, or subscribe by email and have my posts sent in a daily email.
