PHP | get_declared_classes() Function

The get_declared_classes() function is an inbuilt function in PHP which is used to return an array with the name of the defined classes. The user array containing the list of all the system-defined(for example, PDO, XML reader, etc) and user-defined classes in the present script. No parameters are given to this function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class gfg {
      
    function add () {
        $a = 9 + 2 ;
    }
}
  
class gfg1 {
    function tr() {
        echo (4);
    }
}
  
print_r(get_declared_classes());
?>

chevron_right


Output:

Array
(
    [0] => stdClass
    [1] => Exception
    [2] => ErrorException
    [3] => Error
    [4] => ParseError
    . . .
    [155] => Ds\PriorityQueue
    [156] => Ds\Pair
    [157] => gfg
    [158] => gfg1
)

Sorting the list: Finding a particular class in such a big list can be difficult. But it can be easier if the list is sorted alphabetically. It can be sorted through the function sort().

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$sorted = get_declared_classes();
  
sort($sorted);
  
print_r($sorted);
?>

chevron_right


Output:

Array
(
    [0] => AppendIterator
    [1] => ArithmeticError
    [2] => ArrayIterator
    [3] => ArrayObject
    [4] => AssertionError
    . . .
    [152] => XMLWriter
    [153] => __PHP_Incomplete_Class
    [154] => finfo
    [155] => php_user_filter
    [156] => stdClass
)

Reference: https://www.php.net/manual/en/function.get-declared-classes.php

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.