tags:

views:

69

answers:

1
+2  A: 

Perhaps it is easier to use the PHP tokenizer instead of a regular expression. Simply walk over the list of tokens looking for T_NAMESPACE and T_CLASS.

Example (untested):

$map = array();
$tokens = token_get_all($source_code);
$namespace = 'GLOBAL';
foreach ($tokens as $token) {
    if (!is_string($token)) {
        list($id, $text) = $token;
        if ($id == T_NAMESPACE) {
            $namespace = $text;
        }
        if ($id == T_CLASS) {
            $map[$namespace] = $text;
        }
    }
}
print_r($map);
Sander Marechal