php获取函数参数,获取类里面的方法名
2017-05-25 12:23
856 查看
有时候我们需要获取函数需要传入的参数,可以利用php的反射函数获取,或者类里面的所有公开的方法。
1、获取函数参数名称:
function getFucntionParameterName($func) { $ReflectionFunc = new \ReflectionFunction($func); $depend = array(); foreach ($ReflectionFunc->getParameters() as $value) { $depend[] = $value->name; } return $depend; } function test($a, $c, $b, $d = 20) { } $paramName = getFucntionParameterName('test'); print_r($paramName);
结果如下:
2、获取类里面的所有公共方法名:
<?php class GetNames { /** * 获取一个函数的依赖 * @param string|callable $func * @param array $param 调用方法时所需参数 形参名就是key值 * @return array 返回方法调用所需依赖 */ function getFucntionParameter($func, $param = []) { if (!is_array($param)) { $param = [$param]; } $ReflectionFunc = new \ReflectionFunction($func); $depend = array(); foreach ($ReflectionFunc->getParameters() as $value) { if (isset($param[$value->name])) { $depend[] = $param[$value->name]; } elseif ($value->isDefaultValueAvailable()) { $depend[] = $value->getDefaultValue(); } else { $tmp = $value->getClass(); if (is_null($tmp)) { throw new \Exception("Function parameters can not be getClass {$class}"); } $depend[] = $this->get($tmp->getName()); } } return $depend; } //获取方法里面的参数名 function getFucntionParameterName($func) { $ReflectionFunc = new \ReflectionFunction($func); $names = array(); foreach ($ReflectionFunc->getParameters() as $value) { $names[] = $value->name; } return $names; } private function _test($a, $c, $b, $d = 20) { } } function test1($a, $b, $c) { } $new = new GetNames(); $names = $new->getFucntionParameterName('test1'); $methords = get_class_methods('GetNames'); echo "<pre>"; print_r($names); print_r($methords); echo "</pre>";
参考:
http://www.php.net/manual/zh/book.reflection.php相关文章推荐
- php中函数的定义,调用,及获取参数的方法。和位运算等。
- PHP实现动态获取函数参数的方法示例
- PHP获取数组长度的方法 函数参数的比较
- php获取当前网址url并替换参数或网址的方法
- 详解php获取url地址栏及各种参数的方法
- PHP中CURL方法curl_setopt()函数的参数
- PHP 获取参数的方法
- php获取/检查当前类名函数名的方法
- js 获取函数的所有参数和遍历JavaScript某个对象所有的属性名称和值的方法
- PHP中CURL方法curl_setopt()函数的一些参数
- PHP中CURL方法curl_setopt()函数的参数
- 通过函数参数获取新创建指针的三种方法
- php中从结果集获取一条数据和所有数据的方法函数
- PHP获取函数参数数组
- php获取当前网址url并替换参数或网址的方法
- php 获取可变函数参数的函数
- php获取字符串的编码格式的方法(函数)
- php全面获取url地址栏参数多种方法:
- php获取当前类名函数名的方法