PHP gets the function parameter, gets the method name ____ function inside the class

Source: Internet
Author: User
Tags php class reflection
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.