Get Classes In File

If you need to get the classes defined in a specific file, you could use a RegExp or other simple parsing. You might even have luck comparing the results of a get_declared_classes() prior and after including the file. The most elegant solutions is the one I found over at stackoverflow.com:

$tokens = token_get_all( file_get_contents('c2.php') );
$class_token = false;
foreach ($tokens as $token) {
    if ( !is_array($token) ) continue;
    if ($token[0] == T_CLASS) {
        $class_token = true;
    } else if ($class_token && $token[0] == T_STRING) {
        echo "Found class: $token[1]\n";
        $class_token = false;
    }
}

This one uses the internal PHP parser to tokenize the file. So any valid PHP syntax is recognized and will return the correct class names.


 
snippets/php/get-classes-in-file.txt · Last modified: 2009-07-13 17:05.17 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA