0

is there a way to achieve this in PHP?

echo list_args('a_user_defined_function_name_here_such_as_say_hello');

and this outputs something like

$first_name
$last_name

for a function defined as;

function say_hello($first_name, $last_name){
     echo "Hello $first_name $last_name";
}

So basically, what I'm looking for is a function explainer or something of that sort... & if that thing can get into a php doc based comment extractor. that would be even better..

1

2 Answers 2

3

You could use the ReflectionFunction class to do this:

function list_args($name) {
    $list = "";
    $ref = new ReflectionFunction($name);
    foreach ($ref->getParameters() as $param) {
        $list .= '$' . $param->getName() . "\n"; 
    }
    return $list;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tim.. that really does it... I was at us2.php.net/manual/en/reflection.examples.php page, there I see some more options... But they got their examples based on shell calls. How do I make a piece like $ php --rf strlen work in a PHP page... 5.3+
1

You can try ReflectionFunction.

function list_args($function) {
    $func = new ReflectionFunction($function);
    $res = array();
    foreach ($func->getParameters() as $argument) {
        $res[] = '$' . $argument->name;   
    }
    return $res;
}
print_r(list_args('say_hello')); // outputs Array ( [0] => $first_name [1] => $last_name )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.