这是一段从PHP最牛的框架中摘取的代码,对于一些强化PHP代码预处理功能非常有用。
<?php function find_class($file) { $class = false; $namespace = false; $tokens = token_get_all(file_get_contents($file)); if (1 === \count($tokens) && T_INLINE_HTML === $tokens[0][0]) { throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file)); } for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; if (!isset($token[1])) { continue; } if (true === $class && T_STRING === $token[0]) { return $namespace.'\\'.$token[1]; } if (true === $namespace && T_STRING === $token[0]) { $namespace = $token[1]; while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], [T_NS_SEPARATOR, T_STRING])) { $namespace .= $tokens[$i][1]; } $token = $tokens[$i]; } if (T_CLASS === $token[0]) { // Skip usage of ::class constant and anonymous classes $skipClassToken = false; for ($j = $i - 1; $j > 0; --$j) { if (!isset($tokens[$j][1])) { break; } if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) { $skipClassToken = true; break; } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT])) { break; } } if (!$skipClassToken) { $class = true; } } if (T_NAMESPACE === $token[0]) { $namespace = true; } } return false; }
使用:
<?php namespace SomeNamespace; class A { } var_dump(find_class(__FILE__));
得到:
string(15) "SomeNamespace\A"