在将文件包含在FileReflection()之前,有没有办法验证文件是否包含类,接口或函数?

We've got a tree traversal loop running which identifies PHP files all well and good, but for each file we run PHP's FileReflection against it which requires the file to be included first. For the purpose of this script, we're only interested in files that contain Class, Interface or Function declarations.

Is there a way to identify whether a file includes a Class, Interface or Function declaration before running include() on it?

My guess is that it'll have to be a regex against the get_file_contents() value, but I was hoping for a more elegant (read: consistent and reliable) solution.

Found the answer myself, for those interested...

$tokens = token_get_all(file_get_contents($file,TRUE)); $valid = FALSE; foreach($tokens as $token) { if (isset($token[0]) && in_array($token[0],array(T_CLASS,T_FUNCTION,T_INTERFACE))) { $valid = TRUE; break; } } if (!$valid) { return NULL; } include_once $file; $reflection = new FileReflection($file); ...