Sunday, December 21, 2025
HomeLanguagesPHP | get_declared_classes() Function

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.




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


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().




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


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

RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS