[英]PHP: Documentation of function list
我有一个文件,其中包含用户定义的函数和CamelCase名称:
// some useful comments
function functionOne(){
return false;
}
// some other useful comment
function addTwoNumbers($x,$y)
{
return 5;
}
...
我想输出这些按名称排序的函数
addTwoNumbers($x,$y)
2 parameters
Comment: some other useful comment
functionOne()
0 parameters
Comment: some useful comments
到目前为止,我得到了一个像这样的函数名列表:
include '../includes/functions.php';
$allFunctions = get_defined_functions();
$userFunctions = $allFunctions['user'];
sort($userFunctions);
foreach ($userFunctions as $functionName) {
echo $functionName. "<br>";
}
我的主要问题是我不知道如何显示每个函数的参数个数和参数变量名。
进一步(但我可以忍受)函数名称只显示小写字母,所以我无法在CamelCase中读取它们。
最后,评论当然没有显示出来。 我在考虑用数组编写它们$comments['functionOne']="some useful comments"
。
所以我的主要问题是如何获得参数变量名称和数字。
要获得参数编号和名称,可以在PHP中使用Reflection
:
function getFunctionArgumentNames($functionName) {
$reflection = new ReflectionFunction($functionName);
$argumentNames= array();
foreach ($reflection ->getParameters() as $parameter) {
$argumentNames[] = $parameter->name;
}
return $argumentNames;
}
然后你将拥有你的参数名称,返回的数组的长度将给你多少。
ReflectionClass
ReflectionObject
Reflection的另一个常见用途是创建文档。 编写关于大型框架或应用程序的每个类的每个方法的文档都是极其劳动密集的。 相反,Reflection可以自动为您生成文档。 它通过检查每个方法,构造函数和类来确定进入和出现的内容。
使用ReflectionFunction类
例如:
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
print $param;
}
ReflectionFunction类报告有关函数的信息。 查看更多http://php.net/manual/en/class.reflectionfunction.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.