十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
小编给大家分享一下php获取方法的注释,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联专业为企业提供新和网站建设、新和做网站、新和网站设计、新和网站制作等企业网站建设、网页设计与制作、新和企业网站模板建站服务,10多年新和做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
php获取方法的注释:首先打开相应的PHP文件;然后通过php中的反射机制,获取该类的文档注释;最后通过获取其所有的方法,获取方法的注释即可。
php反射获取类和方法中的注释
通过php中的反射机制,获取该类的文档注释,再通过获取其所有的方法,获取方法的注释
所用到的主要类及其方法
ReflectionClass ReflectionClass::getDocComment ReflectionClass::getMethods $method->getName() $method->getDocComment(); $method->isProtected(); $method->getParameters(); $param->getName(); $param->isDefaultValueAvailable(); $param->getDefaultValue()
测试类如下:
test.php
getDocComment ();
//解析类的注释头
$parase_result = DocParserFactory::getInstance()->parse ( $doc );
$class_metadata = $parase_result;
//输出测试
var_dump ( $doc );
echo "\r\n";
print_r( $parase_result );
echo "\r\n-----------------------------------\r\n";
//获取类中的方法,设置获取public,protected类型方法
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC + ReflectionMethod::IS_PROTECTED + ReflectionMethod::IS_PRIVATE);
//遍历所有的方法
foreach ($methods as $method) {
//获取方法的注释
$doc = $method->getDocComment();
//解析注释
$info = DocParserFactory::getInstance()->parse($doc);
$metadata = $class_metadata + $info;
//获取方法的类型
$method_flag = $method->isProtected();//还可能是public,protected类型的
//获取方法的参数
$params = $method->getParameters();
$position=0; //记录参数的次序
foreach ($params as $param){
$arguments[$param->getName()] = $position;
//参数是否设置了默认参数,如果设置了,则获取其默认值
$defaults[$position] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL;
$position++;
}
$call = array(
'class_name'=>$class_name,
'method_name'=>$method->getName(),
'arguments'=>$arguments,
'defaults'=>$defaults,
'metadata'=>$metadata,
'method_flag'=>$method_flag
);
print_r($call);
echo "\r\n-----------------------------------\r\n";
}function.php【推荐学习:《PHP视频教程》】
parse ( $php_doc_comment );
}
/**
* Class DocParserFactory 解析doc
*
* @example
* DocParserFactory::getInstance()->parse($doc);
*/
class DocParserFactory{
private static $p;
private function DocParserFactory(){
}
public static function getInstance(){
if(self::$p == null){
self::$p = new DocParser ();
}
return self::$p;
}
}TestClass.php
DocParser.php 该类源自一个开源项目
* @author R.Arul Kumaran* @copyright 2010 Luracast * @license http://www.gnu.org/licenses/ GNU General Public License * @link https://github.com/murraypicton/Doqumentor */ class DocParser { private $params = array (); function parse($doc = '') { if ($doc == '') { return $this->params; } // Get the comment if (preg_match ( '#^/\*\*(.*)\*/#s', $doc, $comment ) === false) return $this->params; $comment = trim ( $comment [1] ); // Get all the lines and strip the * from the first character if (preg_match_all ( '#^\s*\*(.*)#m', $comment, $lines ) === false) return $this->params; $this->parseLines ( $lines [1] ); return $this->params; } private function parseLines($lines) { foreach ( $lines as $line ) { $parsedLine = $this->parseLine ( $line ); // Parse the line if ($parsedLine === false && ! isset ( $this->params ['description'] )) { if (isset ( $desc )) { // Store the first line in the short description $this->params ['description'] = implode ( PHP_EOL, $desc ); } $desc = array (); } elseif ($parsedLine !== false) { $desc [] = $parsedLine; // Store the line in the long description } } $desc = implode ( ' ', $desc ); if (! empty ( $desc )) $this->params ['long_description'] = $desc; } private function parseLine($line) { // trim the whitespace from the line $line = trim ( $line ); if (empty ( $line )) return false; // Empty line if (strpos ( $line, '@' ) === 0) { if (strpos ( $line, ' ' ) > 0) { // Get the parameter name $param = substr ( $line, 1, strpos ( $line, ' ' ) - 1 ); $value = substr ( $line, strlen ( $param ) + 2 ); // Get the value } else { $param = substr ( $line, 1 ); $value = ''; } // Parse the line and return false if the parameter is valid if ($this->setParam ( $param, $value )) return false; } return $line; } private function setParam($param, $value) { if ($param == 'param' || $param == 'return') $value = $this->formatParamOrReturn ( $value ); if ($param == 'class') list ( $param, $value ) = $this->formatClass ( $value ); if (empty ( $this->params [$param] )) { $this->params [$param] = $value; } else if ($param == 'param') { $arr = array ( $this->params [$param], $value ); $this->params [$param] = $arr; } else { $this->params [$param] = $value + $this->params [$param]; } return true; } private function formatClass($value) { $r = preg_split ( "[\(|\)]", $value ); if (is_array ( $r )) { $param = $r [0]; parse_str ( $r [1], $value ); foreach ( $value as $key => $val ) { $val = explode ( ',', $val ); if (count ( $val ) > 1) $value [$key] = $val; } } else { $param = 'Unknown'; } return array ( $param, $value ); } private function formatParamOrReturn($string) { $pos = strpos ( $string, ' ' ); $type = substr ( $string, 0, $pos ); return '(' . $type . ')' . substr ( $string, $pos + 1 ); } } 以上是“php获取方法的注释”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
文章题目:php获取方法的注释
地址分享:http://www.zsjierui.cn/article/iidshe.html