How to get the name of all user classes?

Asked

Viewed 162 times

5

The function get_declared_classes magpie ALL the classes defined in PHP, both from the internal PHP library and from the user.

I wanted a role for classes like: get_defined_functions, that separates PHP core functions from user functions.

I think we can do with Reflection but I believe there is another option.

How to pick up classes defined by user in PHP?

  • There are some comments in the documentation that if you use the function before declaring the class, it will not appear in the list, so you could do a diff of the results, but I tested and appears even before declaring it...

  • 1

    It would be better to log your Uploader.

  • I tried a test here with array_diff(get_declared_classes(), spl_classes()), but some native classes were returned

  • Taking all user classes is very common in frameworks, but they traverse the directories by opening and tokenizing the files to get the class names. If you are interested I can post a reply showing how to do

  • Sometimes if you aim to get user-defined classes, more practical ways can arise to get what you need. What do you need it for?

  • Need for debug questions... (information).

Show 1 more comment

1 answer

3


So far I’ve found no other way:

You can do it for Reflection:

        $classes = get_declared_classes();

        foreach($classes as $className){
            $reflection = new \ReflectionClass($className);

            if( $reflection->isUserDefined() ){
                echo $className.'<br>';
            }
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.