sometimes we need to get the parameters that the function needs to pass in, and we can use the reflection function of PHP to get it, or all the open methods inside the class.
1. Get function parameter name:
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 =) {
}
$paramName = getfucntionparametername (' test ');
Print_r ($paramName);
The results are as follows:
2, get all the public method names inside the class:
<?php class GetNames {/** * Gets the dependency of a function * @param string|callable $func * @param array $param Calling a method requires the parameter argument name to be the key value
* @return Array to return method calls dependent on/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; }//obtainedtakes the parameter name function Getfucntionparametername ($func) {$ReflectionFunc = new \reflectionfunction ($func) in the method;
$names = Array ();
foreach ($ReflectionFunc->getparameters () as $value) {$names [] = $value->name;
return $names;
The Private function _test ($a, $c, $b, $d =) {}} 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>";
Reference:
http://www.php.net/manual/zh/book.reflection.php