use get_declared_class() to only ouput that classes I declared not the ones PHP does automatically

The Reflection API can detect whether a class is internal or not. ReflectionClass::isInternal Checks whether the class is internal, as opposed to user-defined:

$userDefinedClasses = array_filter(
    get_declared_classes(),
    function($className) {
        return !call_user_func(
            array(new ReflectionClass($className), 'isInternal')
        );
    }
);

The code above will check and remove each class returned by get_declared_classes that is internal, leaving only the user defined classes. This way, you dont need to create and maintain an array of internal classes as was suggested elsewhere on this page.


There is no built-in function to achieve this, but you can get_declared_classes just before you declare anything and store it in global variable, say $predefinedClasses. Then, where you need use:

print_r(array_diff(get_declared_classes(), $predefinedClasses));

theres no directly built-in possibility for this, but you can do the following:

  1. at the very start of your script, call get_declared_classes() and savve it to a variable like $php_classes
  2. after loading your classes, call get_declared_classes() again and use array_diff() to filter out the $php_classes - the result is a list of your own classes.

    // start
    $php_classes = get_declared_classes();
    
    // ...
    // some code loading/declaring own classes
    // ...
    
    // later
    $my_classes = array_diff(get_declared_classes(), $php_classes);
    

Tags:

Php